本文介绍了如何组合这两个SQL语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个 SQL 查询,它们都获取不同 ID 的计数.
I have 2 SQL queries both of which get the counts for different IDs.
select @cntCM_CMQ = count(*)
from dbo.CaseWorkflow cw
join vew_CasePersonnelSystemIDs vcps on cw.ID_Case = vcps.ID_Case
join dbo.WorkflowStates ws on ws.ID_WorkflowState = cw.ID_WorkflowState
where CMSUID = @nSUID and ws.ID_WorkflowType = 3 -- CMQ
select @cntCM_PRWK = count(*)
from dbo.CaseWorkflow cw
join vew_CasePersonnelSystemIDs vcps on cw.ID_Case = vcps.ID_Case
join dbo.WorkflowStates ws on ws.ID_WorkflowState = cw.ID_WorkflowState
where CMSUID = @nSUID and ws.ID_WorkflowType = 1 -- PAPERWORK
似乎我应该能够将它们组合成一个单一的选择(也许使用CASE
语句),但我似乎无法破解它.
It seems that I should be able to combine them into a single select (perhaps with a CASE
statement), but I can't seem to crack it.
推荐答案
类似的事情?
select sum(case when ws.ID_WorkflowType = 1 then 1 else 0 end) as cntCM_PRWK
, sum(case when ws.ID_WorkflowType = 3 then 1 else 0 end) as cntCM_CMQ
from dbo.CaseWorkflow cw
join vew_CasePersonnelSystemIDs vcps on cw.ID_Case = vcps.ID_Case
join dbo.WorkflowStates ws on ws.ID_WorkflowState = cw.ID_WorkflowState
where CMSUID = @nSUID
这篇关于如何组合这两个SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!