SQL Quer

chandan m
chandan m used Ask the Experts™
on
Hi i am working on aging bucket in database with two table, where TotalBal is <> 0  and need to  sum the TotalBal based on:
1.       CBO from Client table  (joined from client code to PK_Client)
2.       30 day bucket grouping based on number of days from SetupDate (on Debtor) and current date.  So it depends on where that Setup Date falls in relation to today.   For example if Setup Date is 3/1/17 it would fall in the ‘0-30’  bucket.  If the Setup date is 2/1/17 it would fall in the ’31-60’ bucket.  There would be 7 buckets altogether as 0-30, 31-60, 61-90, 91-120, 121-150, 151-180, Over 180. I need the output as below, can anybody help me with the solution

      
                      Aging bucket
CBO          0-30  31-60 61-90 etc……up to Over 180 days
CBO1
CBO2
CBO3
etc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
E.g. something like

SELECT  cboColumn ,
        CASE WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 0 AND 30 THEN '0-30'
             WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 31 AND 50 THEN '31-50'
             WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 51 AND 70 THEN '51-70'
             ELSE 'rest'
        END ,
        COUNT(*)
FROM    yourTables
GROUP BY cboColumn ,
        CASE WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 0 AND 30 THEN '0-30'
             WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 31 AND 50 THEN '31-50'
             WHEN DATEDIFF(DAY, SetupdDate, GETDATE()) BETWEEN 51 AND 70 THEN '51-70'
             ELSE 'rest'
        END; 

Open in new window

Modify as per requirement....couldn't test it.

select cboColumn
  ,sum([bucket1]) [0-30]
  ,sum([bucket2]) [31-60]
  ,sum([bucket3]) [61-90]
  ,sum([bucket4]) [91-120]
  ,sum([bucket5]) [121-150]
  ,sum([bucket6]) [151-180]
  ,sum([bucket7]) [Over 180]
from
(
  select d.cboColumn
    ,sum(case when datediff(day, d.SetupDate, getDate()) between   0 and  30 then d.totalBal else 0 end) [bucket1]
    ,sum(case when datediff(day, d.SetupDate, getDate()) between  31 and  60 then d.totalBal else 0 end) [bucket2]
    ,sum(case when datediff(day, d.SetupDate, getDate()) between  61 and  90 then d.totalBal else 0 end) [bucket3]
    ,sum(case when datediff(day, d.SetupDate, getDate()) between  91 and 120 then d.totalBal else 0 end) [bucket4]
    ,sum(case when datediff(day, d.SetupDate, getDate()) between 121 and 150 then d.totalBal else 0 end) [bucket5]
    ,sum(case when datediff(day, d.SetupDate, getDate()) between 151 and 180 then d.totalBal else 0 end) [bucket6]
    ,sum(case when datediff(day, d.SetupDate, getDate()) > 180 then d.totalBal else 0 end) [bucket7]
  from debtor d
  left outer join client c on c.clientCode = d.clientCode
  where d.totalBal != 0
  group by d.cboColumn, d.SetupDate
) s
group by cboColumn

Open in new window

Author

Commented:
When i run the query I am getting the CBO value as NUll, Below is the output

cbo      0-30      31-60      61-90      91-120      121-150      151-180      Over 180
NULL      226107712.30      152636084.32      155731048.91      111809695.53      86786327.37      69392481.03      1624400142.78

The output is not getting grouped by CBO's, Can u please explain the query
I obviously have no idea about your data. You either show table structure of two involved tables, their joining keys and some sample data from both. Preferably a script which I can execute and generate those tables quickly at my end.

Or you will need to modify the query as per your requirement. I assume you are pretty familiar with how tables / columns related to each other.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial