Possible Duplicate:
How to combine these two SQL statements?
I have 2 SQL queries. They get counts based the variable equals either CMSUID or CASUID value in the table. Otherwise, they are exactly the same. How can I combine them into a single query, perhaps using a CASE statement.
select @cntCM_CNF = count(distinct(c.ID_Case))
from dbo.Cases c
join dbo.vew_CasePersonnelSystemIDs vcps on c.ID_Case = vcps.ID_Case
join hearings h on c.ID_Case = h.ID_Case
where vcps.CMSUID = @nSUID
and h.HearingDate > getdate()
and h.OnCalendar = 1 and h.Scheduled = 1
and dbo.fn_HearingConfirmedNoHOs(h.ID_Hearing) < 2
select @cntCC_CNF = count(distinct(c.ID_Case))
from dbo.Cases c
join dbo.vew_CasePersonnelSystemIDs vcps on c.ID_Case = vcps.ID_Case
join hearings h on c.ID_Case = h.ID_Case
where vcps.CASUID = @nSUID
and h.HearingDate > getdate()
and h.OnCalendar = 1 and h.Scheduled = 1
and dbo.fn_HearingConfirmedNoHOs(h.ID_Hearing) < 2
I can’t figure out how to combine them because the result is a count of distinct items. Not sure how to make that happen.
is it…