SQL查询:计数,按日期和年份分组多列

时间:2016-10-03 09:26:42

标签: sql-server

我有一张表如下:

Date       | Type |
2016/04/01    A
2016/04/01    B
2016/04/02    B
2016/05/07    A 
   ...       ...

我尝试创建一个查询,按月/年计算每个类型的出现次数:

DateByYearMonth  |  A  |  B  |
04/2016             1     2
05/2016             1     0
...

我尝试了很多东西,但我没有想要的东西。我的“基本查询”是:

SELECT
  COUNT(1) AS A
FROM DBase.dbo.MyTable
WHERE [Type] = 'A'
AND Date BETWEEN '20140101' AND '20160930'
GROUP BY YEAR(Date),
         MONTH(Date)

有人可以帮我吗?

6 个答案:

答案 0 :(得分:3)

所以你可以做这样的事情,使用按年/月数据分组的PIVOT。

当然,你必须在PIVOT和主要选择中为你的列添加n个所需的数据

select 
   year, 
   month, 
   coalesce(A, 0) as A, 
   coalesce(B, 0) as B
from 
    (select  
       [Type], 
       Year([Date]) as year, 
       Month([Date]) as month, count(*) c
     from tt1
     group by [Type], Year([Date]), Month([Date])
     ) t

PIVOT(sum(c)
      for [Type] in ([A], [B])
) as pvt

答案 1 :(得分:2)

试试这个,

SELECT CONVERT (VARCHAR(7), [date], 20) AS DATEBYYEARMONTH, 
           Sum(CASE 
                 WHEN type = 'A' THEN 1 
                 ELSE 0 
               END)                         A, 
           Sum(CASE 
                 WHEN type = 'B' THEN 1 
                 ELSE 0 
               END)                         B 
    FROM   #temp 
    GROUP  BY CONVERT (VARCHAR(7), [date], 20)

答案 2 :(得分:2)

最简单的方法是使用SELECT CONCAT(MONTH(Date), '/', YEAR(Date)) AS YM , COUNT(CASE WHEN type = 'A' THEN 1 END) AS [A] , COUNT(CASE WHEN type = 'B' THEN 1 END) AS [B] FROM SomeTable GROUP BY MONTH(Date) , YEAR(Date) 条件计算:

IIMWriter

根据您的数据样本输出:

http://jsfiddle.net/T78Hd/138/

答案 3 :(得分:1)

您可以尝试这样的查询

;WITH cte
AS (SELECT
  concat(RIGHT('00' + CONVERT(varchar(2), DATEPART(mm, date)), 2), '/', DATEPART(yyyy, date)) AS dt,
  type,
  COUNT(*) AS Cnt
FROM dates1
GROUP BY concat(RIGHT('00' + CONVERT(varchar(2), DATEPART(mm, date)), 2), '/', DATEPART(yyyy, date)),
         type)
SELECT
  dt,
  ISNULL([A], 0) AS [A],
  ISNULL([B], 0) AS [B]
FROM cte
PIVOT (MAX(cnt) FOR type IN ([A], [B])) P

答案 4 :(得分:1)

我推荐了@RaphaëlAlthau的回答。但是,如果你想让它变得动态,请尝试这个。

DECLARE @TYPES VARCHAR(MAX),
    @TYPESELECT VARCHAR(MAX)        
SELECT @TYPES = STUFF((SELECT ','+ QUOTENAME(TYP) FROM T1 GROUP BY TYP FOR XML PATH('')),1,1,'') ;
SELECT @TYPESELECT = STUFF((SELECT ','+ 'ISNULL('+QUOTENAME(TYP)+',0) AS '+QUOTENAME(TYP) FROM T1 GROUP BY TYP FOR XML PATH('')),1,1,'') ;

EXEC ('SELECT Mnth, '+ @TYPESELECT +'
FROM 
(SELECT  
   [Typ], 
   CAST(YEAR([DateT]) AS VARCHAR) +''/''+CAST(MONTH([DateT]) AS VARCHAR) AS Mnth, 
   COUNT(DateT) C
 FROM T1
 GROUP BY [TyP], YEAR([DateT]), MONTH([DateT])
 ) t
PIVOT(SUM(C)
  FOR [Typ] IN ('+ @TYPES +')
) AS PVT')

答案 5 :(得分:1)

PIVOT的另一个版本:

SELECT *
FROM (
    SELECT  RIGHT(CONVERT(nvarchar(10),[Date],103),7) as DateByYearMonth,
            [Type],
            COUNT(*) as Tcount
    FROM YourTable
    GROUP BY RIGHT(CONVERT(nvarchar(10),[Date],103),7), [Type]
) as t
PIVOT (
    MAX(Tcount) FOR [Type] IN ([A],[B])
) as pvt

输出:

DateByYearMonth A   B
04/2016         1   2
05/2016         1   NULL