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
chandan mAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
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

0
Nitin SontakkeDeveloperCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chandan mAuthor 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
0
Nitin SontakkeDeveloperCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.