首页 > 解决方案 > 多选语句未执行

问题描述

select  
    ((Select 
          [Project Name], count(*) as 'PP_Constuction'
      from 
          [dbo].[EriSite_Executive_Report] 
      where 
          [Planning Object Name] = 'Construction Start'  
          and Status like 'Pending Predecessors' 
      group by
         [Project Name]), 
     (Select 
          [Project Name], count(*) as 'RTS_Constuction'
      from 
          [dbo].[EriSite_Executive_Report] 
      where 
          [Planning Object Name] = 'Construction Start'  
          and Status like 'Ready%' 
      group by   
          [Project Name]), 
     (Select 
          [Project Name], count(*) as 'Comple_Constuction' 
      from 
          [dbo].[EriSite_Executive_Report] 
      where 
          [Planning Object Name] = 'Construction Start'  
          and Status like 'Completed' 
      group by 
          [Project Name])
 )

我需要项目名称并根据某些条件(包括标题)进行计数。但它给出了问题。我想输出一些类似下面的东西

 Project Name  PP_Constuction RTS_Constuction   Comple_Constuction

标签: sql

解决方案


You can try using conditional aggregation

Select 
    [Project Name], 
    count(case when [Planning Object Name] 
    ='Construction Start'  and Status like 'Pending Predecessors' then 1 end) as 'PP_Constuction',
    count(case when [Planning Object Name] 
    ='Construction Start'  and Status like 'Ready%' then 1 end) as 'RTS_Constuction',
    count(case when [Planning Object Name] 
    ='Construction Start'  then 1 end) as 'Comple_Constuction'
from [dbo].[EriSite_Executive_Report]  
group by [Project Name] 

推荐阅读