sql server动态旋转

时间:2015-03-22 04:04:52

标签: sql sql-server sql-server-2008 pivot

我需要有关总结报告的建议。我有一个如下表(少量样本记录)。我在商店中安装了设备(设备和商店之间的多对多关系)。在一段时间内(假设一年)计算设备列表中的计数(假设300到320)。如果listid存在于list中,则count将被添加到适当的月份。如果deviceid存在于时间段但不在列表中,则其他计数将增加。

我准备了一个动态数据透视查询来获取一年数据的计数。查询正在用于设备列表。 但我很困惑,让其他设备计数。请建议或提供一些输入或动态查询。 提前致谢。抱歉我的英语不好。

  

输入表:
  设备ID,STOREID,saledate
  306,44070,2006-02-02 21:58:29.790
  307,44071,2006-03-02 22:00:08.853
  306,44070,2006-04-02 22:14:36.773
  308,44071,2006-04-02 22:15:31.320
  306,44072,2006-02-18 13:39:18.380
  307,44073,2006-03-18 13:46:55.397
  392,44070,2006-02-18 13:53:47.647
  307,44070,2006-04-18 14:03:23.930
  308,44071,2006-02-19 14:54:06.930
  390,44070,2006-04-12 15:16:51.537
  
  输出1 :(每台设备每月计数)
  设备ID,[二月-06],[2006年3月],[APR-06]
  306,2,0,1
  307,0,2,1
  308,2,0,0
  其他,1,0,1   
  输出2 :(每台设备每月不同的存储数量)
  设备ID,STOREID,[二月-06],[2006年3月],[APR-06]
  306,2,2,0,1   307,3,0,2,1   308,1,2,0,0
  其他,1,1,0,1,

1 个答案:

答案 0 :(得分:0)

尝试此操作并相应更改

DECLARE @COLS VARCHAR(MAX);
DECLARE @COLS_ALIAS VARCHAR(MAX);
DECLARE @SQL NVARCHAR(MAX);

select @cols = STUFF((SELECT  DISTINCT ',' + QUOTENAME(Datename(MONTH, Sale_Date))
                from #Your_table
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'');
select @COLS_ALIAS = STUFF((SELECT  DISTINCT ',ISNULL(Max(' + QUOTENAME(Datename(MONTH, Sale_Date))+ '),0) AS '+ QUOTENAME(Datename(MONTH, Sale_Date))
            from #Your_table
    FOR XML PATH(''), TYPE
    ).value('.', 'NVARCHAR(MAX)') 
,1,1,'');


SET @SQL='SELECT A.DEVICE_ID,
       YEAR,
       RN AS COUNT_DISTINCT,'+@cols+'
FROM   (SELECT TEMP                        AS DEVICE_ID,
               Max(YEAR)                   YEAR,
               Max(COUNT1)                 AS COUNT,'+@COLS_ALIAS+'
        FROM   (SELECT A.Device_id,
                       Count(A.Device_id)         AS COUNT,
                       Count(A.Device_id)         AS COUNT1,
                       Datename(MONTH, Sale_Date) AS MONTH,
                       Year(Sale_Date)            AS YEAR,
                       ( CASE
                           WHEN A.Device_id = 306 THEN ''306''
                           WHEN A.Device_id = 307 THEN ''307''
                           WHEN A.Device_id = 308 THEN ''308''
                           ELSE ''Others''
                         END )                    AS TEMP
                FROM   #Your_table A
                GROUP  BY A.Device_id,
                          Datename(MONTH, Sale_Date),
                          Year(Sale_Date)) A
               PIVOT (Max(COUNT)
                     FOR MONTH IN ('+@COLS+'))PV
        GROUP  BY TEMP)A
       JOIN (SELECT TEMP    AS Device_id,
                    Sum(RN) AS RN
             FROM   (SELECT Device_id,
                            Row_number()
                              OVER(
                                PARtition BY DEvice_id, Store_id
                                ORDER BY device_id) AS RN,
                            ( CASE
                           WHEN Device_id = 306 THEN ''306''
                           WHEN Device_id = 307 THEN ''307''
                           WHEN Device_id = 308 THEN ''308''
                           ELSE ''Others''
                         END )                    AS TEMP
                     FROM   #Your_table)A
             WHERE  RN = 1
             GROUP  BY TEMP)B
         ON A.Device_id = B.Device_id'

EXECUTE sp_executesql @SQL