可能重复:
How to combine these two SQL statements?如何将这2个SELECT合并为一个?
我有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
我不知道如何将它们合并,因为结果是不同项目的计数。不知道如何做到这一点。
你不应该试试。 'WHERE'子句是不同的,这意味着你将无法确定组合查询会导致正确的数据被聚合和返回。 – Oded
不错的发现@marc_s ... – Oded
@marc_s它是相似的 - 事实上,我问过它。但是,解决方案并不相同 – AngryHacker