CTE按结果分组

时间:2014-11-12 10:38:48

标签: tsql pivot common-table-expression

我有一个带有Datetime列的表,我想查看当天每小时是否有记录,然后显示当天缺少的小时,所以我用Pivot编写第一个脚本来查找是否记录每小时,但结果不会按相同的日期对行进行分组,

with test as (SELECT [numero] ,[client] ,[creele] ,[montantPaye] ,[AnneeProduction] ,cast(creele as Date) as Datee ,DATEPART(HOUR, creele) as 'Heure'    FROM [CLMDatabase].[dbo].[Fiches] group by creele, numero, client, montantPaye,AnneeProduction )   select Datee, 
isnull([1],0) as "01h",
isnull([2],0) as "02h",
isnull([3],0) as "03h",
isnull([4],0) as "04h",
isnull([5],0) as "05h",
isnull([6],0) as "06h",
isnull([7],0) as "07h",
isnull([8],0) as "08h",
isnull([9],0) as "09h",
isnull([10],0) as "10h",
isnull([11],0) as "11h",
isnull([12],0) as "12h",
isnull([13],0) as "13h",
isnull([14],0) as "14h",
isnull([15],0) as "15h",
isnull([16],0) as "16h",
isnull([17],0) as "17h",
isnull([18],0) as "18h",
isnull([19],0) as "19h",
isnull([20],0) as "20h",
isnull([21],0) as "21h",
isnull([22],0) as "22h",
isnull([23],0) as "23h",
isnull([00],0) as "00h" from test  pivot ( count (client)for Heure in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15] ,[16],[17] ,[18] ,[19] ,[20],[21],[22],[23],[00])) as pvt order by datee ;

结果是这样的:

日期| 01h | 02h | 03h | 04h ... 03-10.2006 | 1 | 0 | 0 | 0 ... 03-10.2006 | 0 | 0 | 1 | 0 ... 13-11.2006 | 0 | 0 | 0 | 1 ...

而不是:

日期| 01h | 02h | 03h | 04h ... 03-10.2006 | 1 | 0 | 1 | 0 ... 13-11.2006 | 0 | 0 | 0 | 1 ...

1 个答案:

答案 0 :(得分:0)

您可以在子查询或cte所需数据中聚合,然后通过数据透视转置它

 SELECT  Datee ,
        ISNULL([1], 0) AS [01h] ,
        ISNULL([2], 0) AS [02h] ,
        ISNULL([3], 0) AS [03h] ,
        ISNULL([4], 0) AS [04h] ,
        ISNULL([5], 0) AS [05h] ,
        ISNULL([6], 0) AS [06h] ,
        ISNULL([7], 0) AS [07h] ,
        ISNULL([8], 0) AS [08h] ,
        ISNULL([9], 0) AS [09h] ,
        ISNULL([10], 0) AS [10h] ,
        ISNULL([11], 0) AS [11h] ,
        ISNULL([12], 0) AS [12h] ,
        ISNULL([13], 0) AS [13h] ,
        ISNULL([14], 0) AS [14h] ,
        ISNULL([15], 0) AS [15h] ,
        ISNULL([16], 0) AS [16h] ,
        ISNULL([17], 0) AS [17h] ,
        ISNULL([18], 0) AS [18h] ,
        ISNULL([19], 0) AS [19h] ,
        ISNULL([20], 0) AS [20h] ,
        ISNULL([21], 0) AS [21h] ,
        ISNULL([22], 0) AS [22h] ,
        ISNULL([23], 0) AS [23h] ,
        ISNULL([00], 0) AS [00h]
FROM    ( SELECT    COUNT([client]) AS Client ,
                    CONVERT(DATE, creele) AS Datee ,
                    DATEPART(HOUR, creele) AS [Heure]
          FROM      #t
          GROUP BY  CONVERT(DATE, creele) ,
                    DATEPART(HOUR, creele)
        ) AS newtest PIVOT ( SUM(Client) FOR Heure IN ( [1], [2], [3], [4],
                                                        [5], [6], [7], [8],
                                                        [9], [10], [11],
                                                        [12], [13], [14],
                                                        [15], [16], [17],
                                                        [18], [19], [20],
                                                        [21], [22], [23],
                                                        [00] ) ) AS pvt
ORDER BY Datee;