2014-11-12 2 views
0

У меня есть стол с столбцом Datetime, я хочу проверить, есть ли запись для каждого часа дня, а затем для отображения отсутствующего часа дня, поэтому я пишу первый скрипт с Pivot найти, если это Ther запись каждого часа, но результат не группировать строки по одной и той же даты,CTE 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 ...

ответ

0

вы можете agregate в подзапросе или 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; 
Смежные вопросы