Solved

Ranking query not needs to exclude Zero field values

Posted on 2015-01-31
2
126 Views
Last Modified: 2015-01-31
The other day I was given the solution of how to find the last payment date and amount in a table of receipts:

SELECT     [VisitID], CONVERT(numeric(14, 2), - [Amount]) AS LPA, Type, CONVERT(char, [ReceiptDateTime], 101) AS LPD
FROM         (SELECT     VisitID, Amount, Type, ReceiptDateTime, ROW_NUMBER() OVER (Partition BY VisitID
                       ORDER BY ReceiptDateTime DESC) AS rn
FROM         [livedb].[dbo].[BarCollectionTransactions]) AS derirved
WHERE     Amount <> 0 AND Type = 'R' AND VisitID IS NOT NULL

Open in new window


This SQL codes works pretty well.  However, the data reported for the following VisitID came over as Zero:

VisitID                     Amount      ReceiptDateTime                      Type
A10000141660      0.00      2010-05-11 00:00:00.000      R
A10000141660      -237.50      2010-04-23 00:00:00.000      R

As you can see the latest date had a Zero amount.  I need the ouputted amount to be the (237.50)

How do I modify the code to give me the correct data?

thanks

Glen
0
Comment
Question by:GPSPOW
2 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40581730
try this:

SELECT [VisitID]
      , CONVERT(numeric(14, 2), - [Amount]) AS LPA
      , Type
      , CONVERT(char, [ReceiptDateTime], 101) AS LPD
FROM (SELECT VisitID
               , Amount
               , Type
               , ReceiptDateTime
               , ROW_NUMBER() OVER (Partition BY VisitID ORDER BY ReceiptDateTime DESC) AS rn
         FROM [livedb].[dbo].[BarCollectionTransactions]
         WHERE Amount <> 0 AND Type = 'R' AND VisitID IS NOT NULL ) AS derirved
0
 

Author Closing Comment

by:GPSPOW
ID: 40581757
Thanks

Worked great.

Glen
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question