Solved

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

Posted on 2006-10-24
5
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

707 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