Solved

SQL 2000: Nested Case/Where Statement for summing withing date ranges

Posted on 2006-10-24
5
285 Views
Last Modified: 2012-06-27
I have a table containing financial transactions (stran). I need to create an 'age of debtors' summary query which groups on the customer reference (st_account) and gives the total sum of transaction balances (sum(st_trbal) and then balances within date (st_trdate) range dependant values within 30 days and older than 30, 60, 90 days ie. (sum(st_trbal)) where st_trdate > GETDATE()-30 As AmtCurrent, (sum(st_trbal)) where st_trdate Between GETDATE()-60 And GETDATE()-30 As Amt30, Between GETDATE()-90 And GETDATE()-60 As Amt60, < GETDATE()-90 As Amt90.

I have come up with the following, but the syntax seems all wrong and SQL Server 2000 query builder does not like case so I am writing direct in Query Analyser:

SELECT     st_account AS CustomerRef, SUM(st_trbal) AS TotalBalance, sum(st_trbal) where st_trdate > GETDATE()-30 As AmtCurrent, sum(st_trbal) where st_trdate Between GETDATE()-60 And GETDATE()-30 As Amt30, sum(st_trbal) Where st_trdate Between GETDATE()-90 And GETDATE()-60 As Amt60, sum(st_trbal) Where st_trdate < GETDATE()-90 As Amt90
FROM         dbo.STRAN
GROUP BY st_account

I realise I could build and link views of the Current, 30, 60 and 90 day balance but I'm sure it should be possible to do this as one query on one table using Case or something - I may need to include data from more tables later but this is the basis of the report/query.

Please Help

Bryan
0
Comment
Question by:bcarrolluk
5 Comments
 
LVL 11

Expert Comment

by:lluthien
ID: 17794782
righto.. that looks like a mess..

first off, the order in sql statements:
http://msdn.microsoft.com/library/en-us/tsqlref/ts_sa-ses_9sfo.asp?frame=true

second,
this grouping is not as straightforward as you might think.
you need to create seperate columns for each agegroup,
and the easiest way to achieve that is probably a self-join for each group.

you can then sum over those groups.

0
 
LVL 11

Expert Comment

by:regbes
ID: 17794897
Hi bcarrolluk,

Try this

-- code start

SELECT     st_account AS CustomerRef,
sum(case when  st_trdate > GETDATE()-30 then st_trbal else o end)  As AmtCurrent
sum(case when  st_trdate Between GETDATE()-60 And GETDATE()-30 then st_trbal else o end)  As Amt30
sum(case when  st_trdate Between GETDATE()-90 And GETDATE()-60 then st_trbal else o end)  As Amt60
sum(case when  st_trdate < GETDATE()-90 then st_trbal else o end)  As Amt90

FROM         dbo.STRAN
GROUP BY st_account

-- code end

HTH

R.
0
 
LVL 11

Accepted Solution

by:
regbes earned 125 total points
ID: 17794904
bcarrolluk,


oops missed the commas and o instead of 0

-- code start

SELECT     st_account AS CustomerRef
,sum(case when  st_trdate > GETDATE()-30 then st_trbal else 0 end)  As AmtCurrent
,sum(case when  st_trdate Between GETDATE()-60 And GETDATE()-30 then st_trbal else 0 end)  As Amt30
,sum(case when  st_trdate Between GETDATE()-90 And GETDATE()-60 then st_trbal else 0 end)  As Amt60
,sum(case when  st_trdate < GETDATE()-90 then st_trbal else 0 end)  As Amt90

FROM         dbo.STRAN
GROUP BY st_account

-- code end
0
 
LVL 7

Expert Comment

by:assyst
ID: 17795042
jus c whether this will help u

select CustomerRef,sum([30]) as [30],sum([30-60]) as [30-60],sum([60-90]) as [60-90] from
(select st_account AS CustomerRef,case when datediff(d,st_trdate ,getdate())<30 then st_trbal else 0 end as '30',
case when datediff(d,st_trdate ,getdate())>=30 and datediff(d,st_trdate ,getdate())<60  then st_trbal else 0 end as '30-60',
case when datediff(d,st_trdate ,getdate())>=60 and datediff(d,st_trdate ,getdate())<90  then st_trbal else 0 end as '60-90'
from dbo.STRAN
 )
dt
group by CustomerRef order by CustomerRef
0
 

Author Comment

by:bcarrolluk
ID: 17801824
Cheers Regbes, that's spot on.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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 wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

777 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