可能重复:
如何组合这两个 SQL 语句?
我有 2 个 SQL 查询。他们根据变量等于CMSUID
或CASUID
表中的值来获得计数。否则,它们完全相同。我怎样才能将它们组合成一个查询,也许使用一个CASE
语句。
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
我不知道如何组合它们,因为结果是不同项目的计数。不知道如何做到这一点。