Bands Using Date

Hi ,
 Need help with one report in that using maturity date; i have to get data for different bands by summing set of IDs.  

Bands.
 30 Days        60 Days       90 Days 180 Days 270 Days  1 Years  2 Years        5 Years       >5 Years

.

Table structure is .

ID , Amount , Maturity Date
1 , 500000 , 20110802
2 , 300000 , 20120802
3 , 200000 , 20150802
4 , 600000 , 20150802
5 , 100000 , 20150908


Regards
Syed Ali Shah RashdiT24/Insight Technical ConsultantAsked:
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.

chaauCommented:
Please provide the expected result from the sample data you have provided
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
        *ID                           30 Days         60 Days       90 Days   180 Days            270 Days      1 Years             2 Years        5 Years       >5 Years  * Bands
Product 1 (1)                                                                                                                                                           500000
Product 2  (2)                                                                                                                                                               300000
Product 3  (3)                                     200000
Product 4  (4)                                     600000
Product 5  (5)           100000

Open in new window

  30 Days        60 Days       90 Days 180 Days 270 Days  1 Years  2 Years        5 Years       >5 Years
Product 1 (1)                                                                                              
Product 2
chaauCommented:
It is very easy to build the first query:
SELECT 
   ID
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) <= 30 Then Amount END As [30 Days]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 30 And DateDiff(day, MaturityDate, GetDate()) <= 60 Then Amount END As [60 Days]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 60 And DateDiff(day, MaturityDate, GetDate()) <= 90 Then Amount END As [90 Days]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 90 And DateDiff(day, MaturityDate, GetDate()) <= 180 Then Amount END As [180 Days]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 180 And DateDiff(day, MaturityDate, GetDate()) <= 270 Then Amount END As [270 Days]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 270 And DateDiff(day, MaturityDate, GetDate()) <= 365 Then Amount END As [1 Year]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 365 And DateDiff(day, MaturityDate, GetDate()) <= 365*2 Then Amount END As [2 Years]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 365*2 And DateDiff(day, MaturityDate, GetDate()) <= 365*5 Then Amount END As [5 Years]
  ,CASE WHEN DateDiff(day, MaturityDate, GetDate()) > 365*5 Then Amount END As [5+ years]
FROM Table1

Open in new window

It is unclear what you need to do with these results. How do you need to sum these ids? Can you elaborate

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
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
select event,sum(case when no_of_day between 0 and 30 then amount else 0 end) Days_30,
 sum(case when no_of_day between 31 and 60 then amount else 0 end) Days_60,
 sum(case when no_of_day between 61 and 90 then amount else 0 end) Days_90,
 sum(case when no_of_day between 91 and 180 then amount else 0 end) Days_180,
 sum(case when no_of_day between 181 and 270 then amount else 0 end) Days_270,
 sum(case when no_of_day between 271 and 365 then amount else 0 end) Days_1Yer,
 sum(case when no_of_day between 366 and 730 then amount else 0 end) Days_2Yer,
 sum(case when no_of_day between 731 and 1825 then amount else 0 end) Days_5Yer,
 sum(case when no_of_day >1825 then amount else 0 end) Days_gr5Yer
from
(select [b]Case when category in ('0004','0006','0007','0010') then 'Cash Balances' [/b]
 when category in ('0059','0060','0061','0072') then 'Bank Balances' 
 when category in ('0044','0045','0046','0047') then 'Due From BOJ'
        when category in ('0022','0025','0027','0028','0029','0031','0033') then 'Item in Course of Collection'  
end event,
DateDiff(DD,Getdate(),date1)No_of_day,SUm(Amount)AMount 
from Table
group by Case when category in ('0004','0006','0007','0010') then 'Cash Balances' 
 when category in ('0059','0060','0061','0072') then 'Bank Balances' 
 when category in ('0044','0045','0046','0047') then 'Due From BOJ'
        when category in ('0022','0025','0027','0028','0029','0031','0033') then 'Item in Course of Collection'  
end,DateDiff(DD,Getdate(),date1)
)Tab
group by event

Open in new window


 This is how i am doing it only issue is if it doesn't find any category then it doesn't return me Cash Balances line but i want it to return Cash Balance line with 0  any help with this .

Thanks
chaauCommented:
You can left join it with a fake table that lists all the possible balances, like this:
with events as(
select 'Cash Balances' as event 
union all 
select 'Bank Balances' as event 
union all 
select 'Due From BOJ' as event 
union all 
select 'Item in Course of Collection' as event 
),
Tab as(
select [b]Case when category in ('0004','0006','0007','0010') then 'Cash Balances' [/b]
 when category in ('0059','0060','0061','0072') then 'Bank Balances' 
 when category in ('0044','0045','0046','0047') then 'Due From BOJ'
        when category in ('0022','0025','0027','0028','0029','0031','0033') then 'Item in Course of Collection'  
end event,
DateDiff(DD,Getdate(),date1)No_of_day,SUm(Amount)AMount 
from Table
group by Case when category in ('0004','0006','0007','0010') then 'Cash Balances' 
 when category in ('0059','0060','0061','0072') then 'Bank Balances' 
 when category in ('0044','0045','0046','0047') then 'Due From BOJ'
        when category in ('0022','0025','0027','0028','0029','0031','0033') then 'Item in Course of Collection'  
end,DateDiff(DD,Getdate(),date1))
select events.event, sum(case when Tab.no_of_day between 0 and 30 then Tab.amount else 0 end) Days_30,
coalesce( sum(case when Tab.no_of_day between 31 and 60 then Tab.amount else 0 end), 0) Days_60,
coalesce(  sum(case when Tab.no_of_day between 61 and 90 then Tab.amount else 0 end), 0) Days_90,
coalesce(  sum(case when Tab.no_of_day between 91 and 180 then Tab.amount else 0 end), 0) Days_180,
coalesce(  sum(case when Tab.no_of_day between 181 and 270 then Tab.amount else 0 end), 0) Days_270,
coalesce(  sum(case when Tab.no_of_day between 271 and 365 then Tab.amount else 0 end), 0) Days_1Yer,
coalesce(  sum(case when Tab.no_of_day between 366 and 730 then Tab.amount else 0 end), 0) Days_2Yer,
coalesce(  sum(case when Tab.no_of_day between 731 and 1825 then Tab.amount else 0 end), 0) Days_5Yer,
coalesce(  sum(case when Tab.no_of_day >1825 then Tab.amount else 0 end), 0) Days_gr5Yer
from events LEFT JOIN Tab on events.event = Tab.event
group by events.event

Open in new window

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 2008

From novice to tech pro — start learning today.