Reputation: 499
I wanted to select count (*)
from a table with condition based on type of one filed, consider below code I want something like this in one query:
select
count (*)
from
moniotr as m
case
when m.type = 1
begin
join paramter as p on p.id = m.paramId
where .....
end
when m.type = 2
begin
join aggTable as ag on ag.id = m.addId
where ....
Could you show me how to handle this in one query?
For more clearance, I want the count on monitors for those with type 1 with condition 1 and for type 2 with condition 2 , sum of those counts for two types! thanks
Upvotes: 0
Views: 39
Reputation: 24763
do as 2 separate query and then union the result
select cnt = sum(cnt)
from
(
select cnt = count(*)
from monitor m
inner join paramter as p on p.id = m.paramId
where m.type = 1
union all
select cnt = count(*)
from monitor m
inner join aggTable as ag on ag.id = m.addId
where m.type = 2
) as d
Upvotes: 3