We help IT Professionals succeed at work.

SQL Quer

chandan m
chandan m asked
on
118 Views
Last Modified: 2017-03-16
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

ste5anSenior Developer
CERTIFIED EXPERT

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

Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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
Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions