Link to home
Create AccountLog in
Avatar of Syed Ali Shah Rashdi
Syed Ali Shah Rashdi

asked on

Help with Case in bands.

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



Need Help with  issue i am facing ,This query brings what i need but  if it doesn't find any category then it doesn't return me e.g " Cash Balances" line but i want it to return Cash Balance line with 0's even if it doesnt find category for it ;   any help with this .

Thanks
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>if it doesn't find any category then it doesn't return me e.g " Cash Balances" line
Not quite sure what you mean here, other than the CASE block that has all the categories does not have an ELSE line that assigns a value if the category is not in any of the lists.
Avatar of Syed Ali Shah Rashdi
Syed Ali Shah Rashdi

ASKER

when i run this qury it return me data for 'Bank Balances' , 'Due From BOJ'  and 'Item in Course of Collection'  but nothing for Cash balances as there is no category against it in db  but i want it to give me cash balance also with 0's . so i should have 3 with data and one with 0
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account