SQL如何使用公用表表达式和动态SQL来PIVOT表

时间:2012-12-06 22:37:44

标签: sql sql-server tsql pivot common-table-expression

我有这个SQL查询按月分组搜索:

; with Mth (st, nd) as (
    select DATEADD (M, datediff (m, 0,'2012-09-01'), 0),
            DATEADD (M, DATEDIFF (m, 0, '2012-09-01') + 1, 0)  
    union all
    select DATEADD (m, 1, st),
            DATEADD (m, 1, nd)
    from Mth
    where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
)
select MONTH(Mth.st) Month,
    COUNT(S.QRY_ID) Searches
FROM Mth
LEFT JOIN SEARCHES S
on Mth.st <= S.CREATED
and Mth.nd > S.CREATED
GROUP BY YEAR(Mth.st), MONTH(Mth.st)
ORDER BY 1,2

结果如下所示:

 Month  |  Searches
---------------------
   9    |    21
   10   |    32
   11   |    18

我正在试图弄清楚如何使用PIVOT来实现这一目标:

  9   |   10    |   11
-----------------------
  21  |   32    |   18

有人可以向我解释怎么做吗?

1 个答案:

答案 0 :(得分:5)

您可以使用以下内容:

; with Mth (st, nd) as (
    select DATEADD (M, datediff (m, 0,'2012-09-01'), 0),
            DATEADD (M, DATEDIFF (m, 0, '2012-09-01') + 1, 0)  
    union all
    select DATEADD (m, 1, st),
            DATEADD (m, 1, nd)
    from Mth
    where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
)
select *
from
(
  select MONTH(Mth.st) Month,
      COUNT(S.QRY_ID) Searches
  FROM Mth
  LEFT JOIN SEARCHES S
    on Mth.st <= S.CREATED
    and Mth.nd > S.CREATED
  GROUP BY YEAR(Mth.st), MONTH(Mth.st)
) src
pivot
(
  sum(searches)
  for month in ([9], [10], [11])
) piv

请参阅SQL Fiddle with Demo

如果您有未知的月数进行转换,那么您可以使用与此类似的动态SQL:

; with Mth (st, nd) as (
    select DATEADD (M, datediff (m, 0,'2012-09-01'), 0),
            DATEADD (M, DATEDIFF (m, 0, '2012-09-01') + 1, 0)  
    union all
    select DATEADD (m, 1, st),
            DATEADD (m, 1, nd)
    from Mth
    where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
)
select *
into #dates
from Mth

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

select @cols = STUFF((SELECT  ',' + QUOTENAME(month(st))
                    from #dates
                    group by month(st)
                    order by month(st) 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = ' with Mth (st, nd) as (
                select DATEADD (M, datediff (m, 0,''2012-09-01''), 0),
                        DATEADD (M, DATEDIFF (m, 0, ''2012-09-01'') + 1, 0)  
                union all
                select DATEADD (m, 1, st),
                        DATEADD (m, 1, nd)
                from Mth
                where nd <= DATEADD (m, datediff (m, 0, getdate()), 0)
            )
            select *
            from
            (
              select MONTH(Mth.st) Month,
                  COUNT(S.QRY_ID) Searches
              FROM Mth
              LEFT JOIN SEARCHES S
                on Mth.st <= S.CREATED
                and Mth.nd > S.CREATED
              GROUP BY YEAR(Mth.st), MONTH(Mth.st)
            ) src
            pivot
            (
              sum(searches)
              for month in ('+@cols+')
            ) piv'

execute(@query)

请参阅SQL Fiddle with Demo

相关问题