У меня есть стол с столбцом 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 ...