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
Microsoft SQL Server

Avatar of undefined
Last Comment
Nitin Sontakke

8/22/2022 - Mon
ste5an

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
Nitin Sontakke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Nitin Sontakke

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61