2012-12-30 32 views
0

可能重复:
How to combine these two SQL statements?如何将这2个SELECT合并为一个?

我有2个SQL查询。他们根据变量等于表中的CMSUIDCASUID值获得计数。否则,他们是完全一样的。我怎样才能将它们组合成一个查询,也许使用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 

我不知道如何将它们合并,因为结果是不同项目的计数。不知道如何做到这一点。

+0

你不应该试试。 'WHERE'子句是不同的,这意味着你将无法确定组合查询会导致正确的数据被聚合和返回。 – Oded

+1

不错的发现@marc_s ... – Oded

+0

@marc_s它是相似的 - 事实上,我问过它。但是,解决方案并不相同 – AngryHacker

回答

2

是...

select 
    @cntCM_CNF = count(distinct case when vcps.CMSUID = @nSUID then c.ID_Case else null end), 
    @cntCC_CNF = count(distinct case when vcps.CASUID = @nSUID then c.ID_Case else null end) 
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 
    h.HearingDate > getdate() 
    and h.OnCalendar = 1 and h.Scheduled = 1 
    and dbo.fn_HearingConfirmedNoHOs(h.ID_Hearing) < 2 AND 
    (vcps.CASUID = @nSUID OR vcps.CMSUID = @nSUID) --<<<<added for performance reasons 
+0

+1这就是我的想法。 – BellevueBob

+0

出于性能原因(特别是如果这些列被索引),我会将(vcps.CASUID = @nSUID或vcps.CMSUID = @nSUID)添加到WHERE子句。 –

+0

好的 - 同意 - 我会编辑。我假设这样做的原因是引擎在评估“SELECT”中的内容之前是否有处理'WHERE'子句的倾向? – whytheq

1

看看UNION是如何工作的。

本质上,您将在一列中选择一个计数,0,然后在另一列中选择0。

您可能需要使用UNION ALL

select @cntCM_CNF = count(distinct(c.ID_Case)) AS Col1, 0 As Col2 
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 
UNION ALL 
select 0 as Col1, @cntCC_CNF = count(distinct(c.ID_Case)) AS Col2 
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 
+0

这可行,但它并没有真正将它合并成一个逻辑查询。我试图通过遍历所有这些表格来缓解一些性能问题,而不是两次。 – AngryHacker

+0

@AngryHacker - 不要认为将它们结合起来会使它更具性能。您按不同的字段进行筛选,因此应该在每个查询中使用不同的索引。性能管每个单独。如果您需要关于*的帮助,请提供所用表格的详细Schema,以及关于数据的关系和行为的描述。 – MatBailie

-1

使用Case快于Union All - >因为它会扫描1次,你的表

Union All Is faster Union - >有没有检查取决于您

螺丝Distinct

+0

这不是一个答案,它的评论 – sicKo

+0

“拧'DISTINCT'”......我假定你的意思是把它拿出来;但评论的理由是什么 - 如果'ID_Case'不是唯一的,那么我们如何避免使用'DISTINCT'? – whytheq

相关问题