Link to home
Start Free TrialLog in
Avatar of Syed Ali Shah Rashdi
Syed Ali Shah Rashdi

asked on

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
Avatar of chaau
chaau
Flag of Australia image

Please provide the expected result from the sample data you have provided
Avatar of Syed Ali Shah Rashdi
Syed Ali Shah Rashdi

ASKER

        *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
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia 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
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
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