在SQL语句中计数

时间:2017-10-24 04:31:41

标签: sql sql-server

我有一张包含以下数据的表格:

Comp  ID    Name  Type
-----------------------
AAA   D2222 Jon   BR11
AAA   D2222 Jon   BR12
AAA   D2865 Toe   BR11
BBB   D4151 Sue   BR11
BBB   D4151 Sue   BR12
BBB   D4151 Sue   BR13
CCC   D6080 Pete  BR14
CCC   D6723 Tom   BR13

我想编写我的SQL语句并显示如下表

Comp BR11  BR12  BR13  BR14
---------------------------
AAA   2     1       
BBB   1     1      1    
CCC                1    1

但我只知道选择一种类型,我怎样才能为多种类型做到?

select 
    Comp, count(Type) as BR11
from 
    CCDL 
where 
    Type = 'BR11'
group by 
    Comp

非常感谢!

5 个答案:

答案 0 :(得分:3)

动态支点是最好的方法:

create  table test (Comp varchar(3), ID varchar(10), Name varchar(10), Type varchar(10))

insert into test values ('AAA','D2222','Jon','BR11');            
insert into test values ('AAA','D2222','Jon','BR12');            
insert into test values ('AAA','D2865','Toe','BR11');            
insert into test values ('BBB','D4151','Sue','BR11');            
insert into test values ('BBB','D4151','Sue','BR12');            
insert into test values ('BBB','D4151','Sue','BR13');            
insert into test values ('CCC','D6080','Pete','BR14');           
insert into test values ('CCC','D6723','Tom','BR13');            



DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.Type) 
            FROM test c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Comp, ' + @cols + ' from 
            (
                select Comp
                    , ID
                    , Type
                from test
           ) x
            pivot 
            (
                 count(ID)
                for Type in (' + @cols + ')
            ) p '


execute(@query)

结果是

Comp    BR11    BR12    BR13    BR14
AAA       2       1       0      0
BBB       1       1       1      0 
CCC       0       0       1      1

答案 1 :(得分:2)

您可以使用选择性聚合:

SELECT Comp
     , COUNT(CASE WHEN type = 'BR11' THEN 1 END) br11
     , COUNT(CASE WHEN type = 'BR12' THEN 1 END) br12
     ,  ...
  FROM CCDL
 GROUP BY Comp

有关此内容的更多信息:http://modern-sql.com/feature/filter

它基本上也是一种支点技术:http://modern-sql.com/use-case/pivot

答案 2 :(得分:2)

尝试以下代码

declare @tab table (Comp varchar(50),Id varchar(50),Name varchar(50),Type varchar(50))
insert into @tab 
Select 'AAA','D2222','Jon','BR11' Union ALL
Select 'AAA','D2222','Jon','BR12' Union ALL
Select 'AAA','D2865','Toe','BR11' Union ALL
Select 'BBB','D4151','Sue','BR11' Union ALL
Select 'BBB','D4151','Sue','BR12' Union ALL
Select 'BBB','D4151','Sue','BR13' Union ALL
Select 'CCC','D6080','Pete','BR14'Union ALL
Select 'CCC','D6723','Tom','BR13'
Select *  from 
(Select  type,comp,count(*) cnt from @tab
group by type,Comp

)d
PIVOT
(Sum(Cnt) FOR Type in ([BR11],[BR12],[BR13],[BR14]))p

答案 3 :(得分:1)

尝试使用:将CASESUM一起使用,如下所示:

SELECT Comp, 
    SUM(CASE WHEN type = 'BR11' THEN 1 ELSE 0 END) br11,
    SUM(CASE WHEN type = 'BR12' THEN 1 ELSE 0 END) br12,
    SUM(CASE WHEN type = 'BR13' THEN 1 ELSE 0 END) br13,
    SUM(CASE WHEN type = 'BR14' THEN 1 ELSE 0 END) br14
FROM CCDL
GROUP BY Comp

答案 4 :(得分:1)

你能做什么

here

SELECT Comp, 
    SUM(CASE WHEN type = 'BR11' THEN 1 ELSE 0 END) br11,
    SUM(CASE WHEN type = 'BR12' THEN 1 ELSE 0 END) br12,
    SUM(CASE WHEN type = 'BR13' THEN 1 ELSE 0 END) br13,
    SUM(CASE WHEN type = 'BR14' THEN 1 ELSE 0 END) br14
FROM CCDL
GROUP BY Comp