How to combine these two SQL statements?

前端 未结 5 1260
说谎
说谎 2020-12-20 04:03

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         


        
5条回答
  •  情话喂你
    2020-12-20 04:53

    There's a trick to doing this. essentially you use Case to pick out a 1 value for the rows you are interested in and then Sum the ones to get a count. Case defaults to null if no cases match, which get ignored by Sum

    Select
      @cntCM_CMQ = Sum(Case ws.ID_WorkflowType When 3 Then 1 End),
      @cntCM_PRWK = Sum(Case ws.ID_WorkflowType When 1 Then 1 End) 
    From
      dbo.CaseWorkflow cw 
        inner join 
      vew_CasePersonnelSystemIDs vcps 
        on cw.ID_Case = vcps.ID_Case
        inner join 
      dbo.WorkflowStates ws 
        on ws.ID_WorkflowState = cw.ID_WorkflowState
    Where
      CMSUID = @nSUID
    

提交回复
热议问题