non répétitif Avec ce tableau:SQL 2012 table html avec le chemin xml avec colonne valeurs
declare @table table
(
ID int not null IDENTITY(1,1),
day date,
hour int,
val int
)
insert into @table
values
('2016-06-15', 8, 1),
('2016-06-15', 9, 1),
('2016-06-15', 10, 7),
('2016-06-16', 8, 3),
('2016-06-15', 9, 3),
('2016-06-15', 10, 5)
qui est un résultat d'une requête qui répertorie les valeurs par jour et par heure. J'utilise le code ci-dessous que j'ai trouvé quelque part (je l'utilise mais je ne le comprends pas vraiment) pour générer une table html depuis @table.
SELECT CAST((select 2 [@cellpadding]
,2 [@cellspacing]
,1 [@border]
, 'background-color: rgb(226, 140, 5)' [@style]
,(select th
from (select 'Day' th union all
select 'Hour' th
union all
select 'Val'
) d
for xml path(''),type) tr
,(select (select 'width:260px;vertical-align:top;background-color: white;' [@style], [Day] for xml path('td'),type),
(select 'width:260px;vertical-align:top;background-color: white;' [@style], [Hour] for xml path('td'),type),
(select 'width:260px;vertical-align:top;background-color: white;' [@style], val for xml path('td'),type)
from (
select t.day
, t.Hour
, t.val
FROM @table t
) data
for xml path ('tr'),type)
for xml path('table'), type) AS nvarchar(MAX))
Ce qui me donne une table html comme ceci:
Day Hour Val
2016-06-15 8 1
2016-06-15 9 1
2016-06-15 10 7
2016-06-16 8 3
2016-06-16 9 3
2016-06-16 10 5
Ce que je veux est une table comme ceci:
Day Hour Val
2016-06-15 8 1
9 1
10 7
2016-06-16 8 3
9 3
10 5
-à-dire ne pas répéter même valeur pour les jours, seulement montrer la valeur du jour quand c'est un nouveau jour.
J'ai posté une réponse s'il vous plaît vérifier – StackUser