Link to home
Start Free TrialLog in
Avatar of chandan m
chandan m

asked on

SQL Quer

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
Avatar of ste5an
ste5an
Flag of Germany image

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

ASKER CERTIFIED SOLUTION
Avatar of Nitin Sontakke
Nitin Sontakke
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chandan m
chandan m

ASKER

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.