Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
select p.partnerid,p.name,count(*) as accounts,
sum(case when c.amount is not null then c.amount else 0 end) as amount,
sum(case when c.netamt is not null then c.netamt else 0 end) as netamt,
sum(case when c.netamt - c.amount is not null then c.netamt - c.amount else 0 end) as interest,
sum(case when c.installment = 20 then c.amount else 0 end) as Twenty,
sum(case when c.installment = 10 then c.amount else 0 end) as Ten,
sum(case when c.installment = 5 then c.amount else 0 end) as Five,
sum(case when c.installment = 2 then c.amount else 0 end) as Two
from partnerinfo p left outer join customerinfo c on p.partnerid = c.partnerid
where (c.startdate is null OR (c.startdate >= '2011-3-15' and c.startdate <= '2012-12-30'))
and (c.partnerid is null or c.partnerid) and p.manager = 1
group by p.partnerid


With the above query i was able to fetch only null rows / when the whole row does not exist.

When i changed from
SQL
where (c.startdate is null OR (c.startdate >= '2011-3-15'...)

TO
SQL
where (c.startdate is null OR (c.startdate >= '2012-3-15'...)


But i need to fetch the data as null rows even when the columns are present.

Hi for a better understanding please see the images...

http://i46.tinypic.com/155l4cp.jpg[^]

http://i49.tinypic.com/357ki3b.jpg[^]

Please help me i was stuck hear.


[edit]Links converted to a proper link - OriginalGriff[/edit]
Posted
Updated 20-Jun-12 19:57pm
v4
Comments
Mohibur Rashid 19-Jun-12 21:07pm    
I tried to understand query, but i don't understand what do you want, will you show the table and show us what would be final result?
Mohibur Rashid 21-Jun-12 0:45am    
2011-3-15 should be 2011-03-15

Why you allowed amount to be null? if no value then it should be zero.


why do you want to fetch line with when the startdate == NULL?
c.partnerid is null or c.partnerid does not make sense
varaprasadreddy 21-Jun-12 1:27am    
Hi i should get 0(zero) only when the row is null

1 solution

The WHERE clause affects the whole row - as you have noticed. If you want to fetch the data as zero when you have a out-of-range date, then you need to move the condition from the WHERE clause into each of your individual SUM expressions:
SQL
sum(case when c.amount is not null then c.amount else 0 end) as amount,
...
where (c.startdate is null OR (c.startdate >= '2011-3-15' and c.startdate <= '2012-12-30'))
and (c.partnerid is null or c.partnerid) and p.manager = 1

becomes:
SQL
sum(case when c.amount is not null and not (c.startdate is null OR (c.startdate >= '2011-3-15' and c.startdate <= '2012-12-30')) then c.amount else 0 end) as amount,
...
where (c.partnerid is null or c.partnerid) and p.manager = 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900