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
Syed Ali Shah RashdiT24/Insight Technical ConsultantAsked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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.
0
Syed Ali Shah RashdiT24/Insight Technical ConsultantAuthor Commented:
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
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You're likely going to need ELSE blocks in all your CASE statements, as one or all of them is filtering rows.
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.