Sql 如何添加两个select查询

Sql 如何添加两个select查询,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,以下sql查询是否可行。 我希望在同一个表的不同列中有三个计数,并且只使用一个查询 select COUNT(Status) as attendedcount from Ebrahim_30359715 where Status='Attended' UNION select COUNT(Status) as notallocatedcount from Ebrahim_30359715 where Status='Not Allocated' UNION select COUNT(St

以下sql查询是否可行。
我希望在同一个表的不同列中有三个计数,并且只使用一个查询

select COUNT(Status) as attendedcount from Ebrahim_30359715 where Status='Attended' 
UNION 
select COUNT(Status) as notallocatedcount from Ebrahim_30359715  where Status='Not Allocated'
UNION 
select COUNT(Status) as allocatedbutnotcount from Ebrahim_30359715  where Status='Allocated But Not Attended'
更好地利用

select SUM(case when Status='Attended' then 1 end) as attendedcount,
       SUM(case when Status='Not Attended' then 1 end) as notallocatedcount,
       SUM(case when Status='Allocated But Not Attended' then 1 end) as allocatedbutnotcount
from Ebrahim_30359715
使用
union
时,您必须创建3个不同的列,或者添加一个额外的列,指示存储在第1列中的值。

更好地使用

select SUM(case when Status='Attended' then 1 end) as attendedcount,
       SUM(case when Status='Not Attended' then 1 end) as notallocatedcount,
       SUM(case when Status='Allocated But Not Attended' then 1 end) as allocatedbutnotcount
from Ebrahim_30359715
使用
union
时,您必须创建3个不同的列,或者添加一个额外的列,指示存储在第1列中的值。

更好地使用

select SUM(case when Status='Attended' then 1 end) as attendedcount,
       SUM(case when Status='Not Attended' then 1 end) as notallocatedcount,
       SUM(case when Status='Allocated But Not Attended' then 1 end) as allocatedbutnotcount
from Ebrahim_30359715
使用
union
时,您必须创建3个不同的列,或者添加一个额外的列,指示存储在第1列中的值。

更好地使用

select SUM(case when Status='Attended' then 1 end) as attendedcount,
       SUM(case when Status='Not Attended' then 1 end) as notallocatedcount,
       SUM(case when Status='Allocated But Not Attended' then 1 end) as allocatedbutnotcount
from Ebrahim_30359715

使用
union
时,您必须创建3个不同的列,或者添加一个额外的列,指示存储在第1列中的值。

试试这个。这也适用于新的身份。你不必明确地提到状态

DECLARE @cols AS VARCHAR(max), 
        @sql  AS NVARCHAR(max) 

SELECT @cols = Stuff((SELECT ',' + Quotename(status) 
                      FROM   Ebrahim_30359715
                      GROUP  BY status 
                      FOR xml path(''), type).value('.', 'VARCHAR(MAX)'), 1, 1,'') 

SET @sql = 'select *  from  
                      (select count(1) as cnt ,status
                       from Ebrahim_30359715
                       group by status )
            src pivot (max(cnt) for status in (' + @cols + ')) piv' 

EXEC Sp_executesql 
     @sql 

试试这个。这也适用于新的身份。你不必明确地提到状态

DECLARE @cols AS VARCHAR(max), 
        @sql  AS NVARCHAR(max) 

SELECT @cols = Stuff((SELECT ',' + Quotename(status) 
                      FROM   Ebrahim_30359715
                      GROUP  BY status 
                      FOR xml path(''), type).value('.', 'VARCHAR(MAX)'), 1, 1,'') 

SET @sql = 'select *  from  
                      (select count(1) as cnt ,status
                       from Ebrahim_30359715
                       group by status )
            src pivot (max(cnt) for status in (' + @cols + ')) piv' 

EXEC Sp_executesql 
     @sql 

试试这个。这也适用于新的身份。你不必明确地提到状态

DECLARE @cols AS VARCHAR(max), 
        @sql  AS NVARCHAR(max) 

SELECT @cols = Stuff((SELECT ',' + Quotename(status) 
                      FROM   Ebrahim_30359715
                      GROUP  BY status 
                      FOR xml path(''), type).value('.', 'VARCHAR(MAX)'), 1, 1,'') 

SET @sql = 'select *  from  
                      (select count(1) as cnt ,status
                       from Ebrahim_30359715
                       group by status )
            src pivot (max(cnt) for status in (' + @cols + ')) piv' 

EXEC Sp_executesql 
     @sql 

试试这个。这也适用于新的身份。你不必明确地提到状态

DECLARE @cols AS VARCHAR(max), 
        @sql  AS NVARCHAR(max) 

SELECT @cols = Stuff((SELECT ',' + Quotename(status) 
                      FROM   Ebrahim_30359715
                      GROUP  BY status 
                      FOR xml path(''), type).value('.', 'VARCHAR(MAX)'), 1, 1,'') 

SET @sql = 'select *  from  
                      (select count(1) as cnt ,status
                       from Ebrahim_30359715
                       group by status )
            src pivot (max(cnt) for status in (' + @cols + ')) piv' 

EXEC Sp_executesql 
     @sql 

我必须为我自己尝试,我喜欢。我必须为我自己尝试,我喜欢。我必须为我自己尝试,我喜欢。我必须为我自己尝试,我喜欢。