Solved

Ranking query not needs to exclude Zero field values

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
export sql results to csv 6 35
2016 SQL Licensing 7 41
SQL View nearest date 5 36
Passing Parameter to Stored Procedure 4 24
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

776 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