Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sum Most Recent Amounts

Author  Topic 

schin
Starting Member

1 Post

Posted - 2012-01-27 : 12:53:09
We're running SQL Server 2008R2. We have a database with some tables that have information about clients, which includes fields like ClientID, TransactionDate, TransactionAmount, etc. We have a number of clients who are identified by a number, and each client will usually have multiple transactions on multiple dates. What I am trying to do is put together a script that gives me the total of TransactionAmount only for the most recent TransactionDate. ClientID and TransactionAmount are of type INT (the amount field is stored in cents, so we have to divide by 100 to display the dollar amount). TransactionDate is obviously a Date/Time type, storing only the date with no time information. The data looks something like (sorry for the odd look to the data, I am new to the forum and don't know how to format the text properly yet):

ClientID TransactionAmount TransactionDate
23 12032 2011-10-03 00:00:00.0000
23 218327 2011-10-03 00:00:00.0000
23 0 2011-10-03 00:00:00.0000
23 116052 2011-04-30 00:00:00.0000
88 482591 2011-06-12 00:00:00.0000
88 0 2011-06-12 00:00:00.0000
88 12652 2011-06-12 00:00:00.0000
88 332684 2011-01-16 00:00:00.0000

If this were all of the data, we'd like to total the amount for Client 23 only the transactions on October 3, 2011 and the amount for Client 88 only the transactions on June 12, 2011. For some reason, using the SUM() function as part of the SELECT statement gives me a completely wrong total. Any ideas?



Steve Chin

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-27 : 13:18:30
Either of the following:
SELECT
ClientId,
SUM(y1.TransactionAmount)/100.0 AS TotalDollars
FROM
YourTable y1
CROSS APPLY
(
SELECT MAX(TransactionDate) AS Dt FROM YourTable y2 WHERE y1.ClientId = y2.clientId
) y2
WHERE
y1.TransactionDate = y2.Dt
GROUP BY
ClientId;

SELECT
ClientId,
SUM(y1.TransactionAmount)/100.0 AS TotalDollars
FROM
YourTable y1
WHERE
y1.TransactionDate =
(
SELECT MAX(TransactionDate) FROM YourTable y2
WHERE y2.ClientId = y1.clientId
)
GROUP BY
ClientId;

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-27 : 13:36:39
quote:
Originally posted by schin

We're running SQL Server 2008R2. We have a database with some tables that have information about clients, which includes fields like ClientID, TransactionDate, TransactionAmount, etc. We have a number of clients who are identified by a number, and each client will usually have multiple transactions on multiple dates. What I am trying to do is put together a script that gives me the total of TransactionAmount only for the most recent TransactionDate. ClientID and TransactionAmount are of type INT (the amount field is stored in cents, so we have to divide by 100 to display the dollar amount). TransactionDate is obviously a Date/Time type, storing only the date with no time information. The data looks something like (sorry for the odd look to the data, I am new to the forum and don't know how to format the text properly yet):

ClientID TransactionAmount TransactionDate
23 12032 2011-10-03 00:00:00.0000
23 218327 2011-10-03 00:00:00.0000
23 0 2011-10-03 00:00:00.0000
23 116052 2011-04-30 00:00:00.0000
88 482591 2011-06-12 00:00:00.0000
88 0 2011-06-12 00:00:00.0000
88 12652 2011-06-12 00:00:00.0000
88 332684 2011-01-16 00:00:00.0000

If this were all of the data, we'd like to total the amount for Client 23 only the transactions on October 3, 2011 and the amount for Client 88 only the transactions on June 12, 2011. For some reason, using the SUM() function as part of the SELECT statement gives me a completely wrong total. Any ideas?



Steve Chin



SELECT ClientID,TransactionDate,SUM(TransactionAmount) AS TotalAmt
FROM
(SELECT DENSE_RANK() OVER (PARTITION BY ClientID ORDER BY TransactionDate DESC) AS Rn,*
FROM table
)t
WHERE Rn=1
GROUP BY ClientID,TransactionDate


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-27 : 13:39:10
Why?

Because they are the earliest Dates?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-01-27 : 13:42:38
But mine would perform better if there is an index on ClientID and Transaction Date.

(I am guessing. If I am wrong, I didn't post this, someone must have hacked into my account).
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-01-27 : 17:41:24
mine would runn faster if I used 94 octane...



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -