Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi folks.I have a dataset of financial transactions where the clients incur debt, each taking place on a given date. Something like: Date Debt1. Mr.Turner 38503 $1002. Mr.Johnson 38454 $2003. Mr.Clark 38506 $500The dates are in integer format. I would like to be able to get a spread of the debt for each client based on the age of the debt. Something like: Debt < 30 days Debt < 60 days 1. Mr.Turner $1002. Mr.Johnson $2003. Mr.Clark $500All I've been able to do so far is get the aged debt individually for each given age by doing:Select Client, Sum(Debt) where Date > 38475 Group By ClientSelect Client, Sum(Debt) where Date > 38445 AND Date <= 38475 Group By ClientBut is there a statement that I can use to parse the debt appropriately in a single resultset to include all ages for all clients as per the second illustration above?Thanks!
AndyB13
Aged Yak Warrior
583 Posts
Posted - 2005-03-03 : 12:25:39
Have a look at CASE in BOLSomething like
SELECT Client, SUM(CASE WHEN DATEDIFF(dd,YourDate,GETDATE()) < 30 THEN Debt ELSE 0 END) AS Under30Days, SUM(CASE WHEN DATEDIFF(dd,YourDate,GETDATE()) BETWEEN 30 AND 60 THEN Debt ELSE 0 END) AS 30to60DaysFROM YourTableGROUP BY Client
AndyBeauty is in the eyes of the beerholder
WindChaser
Posting Yak Master
225 Posts
Posted - 2005-03-03 : 15:25:48
That looks absolutely beautiful. I'll try it out. Thanks Andy !!!