简体   繁体   中英

sql server dynamic pivoting

I need suggestion on summary report. I have a table like below(few sample records). I have devices installed in stores(many to many relationship between device and store). calculate count in a list of devices(Assume 300 to 320) over a period of time(assume one year). If deviceid exist in list then count will be added to appropriate month. If deviceid is present in time period but not in the list then others count will increase.

I prepared an dynamic pivot query to get counts for one year data. Query is working for a list of devices. But i am confused to get the other device count. Please suggest or give some inputs or dynamic query. Thanks in advance. Sorry for my poor english.

Input Table:
deviceid,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

Output1:(count per month per device)
deviceid,[Feb-06],[Mar-06],[Apr-06]
306,2,0,1
307,0,2,1
308,2,0,0
Others,1,0 , 1

Output2:(distinct store count per device per month)
deviceid,storeid,[Feb-06],[Mar-06],[Apr-06]
306,2,2,0, 1
307,3,0,2 , 1
308,1,2,0,0
Others,1,1 ,0 , 1

Try this and change accordingly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM