Solved

Ranking query not needs to exclude Zero field values

Posted on 2015-01-31
2
117 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 33

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now