Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-10-24
5
Medium Priority
?
302 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 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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

618 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