Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL如何使用公共表表达式和动态SQL透视表_Sql_Sql Server_Tsql_Pivot_Common Table Expression - Fatal编程技术网

SQL如何使用公共表表达式和动态SQL透视表

SQL如何使用公共表表达式和动态SQL透视表,sql,sql-server,tsql,pivot,common-table-expression,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 M

我有一个按月分组搜索的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
我正试图找出如何使用PIVOT来实现这一点:

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

有人能给我解释一下怎么做吗?

你可以用这样的方法:

 Month  |  Searches
---------------------
   9    |    21
   10   |    32
   11   |    18
; 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:

; 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)

请参见

是的,确实如此,但我可以使用CTE而不是写入月份值9、10、11等吗。。?我正在准备一份最近3个月的报告如果你不知道这些月的值,那么你将不得不使用动态sql在你的sql小提琴中执行这项任务,为什么9月9日是最后一天?因为它被当作一根绳子?@greener现在应该修好了。谢谢。这正是我要找的。