2016-06-30 3 views
0

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.

+0

J'ai posté une réponse s'il vous plaît vérifier – StackUser

Répondre

2

Utilisez la fonction Row_Number() pour afficher les résultats attendus. Essayez comme ceci,

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-16' 
    ,9 
    ,3 
    ) 
    ,(
    '2016-06-16' 
    ,10 
    ,5 
    ) 

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 CASE 
           WHEN row_number() OVER (
             PARTITION BY day ORDER BY day 
             ) = 1 
            THEN t.day 
           ELSE NULL 
           END AS day 
          ,t.Hour 
          ,t.val 
         FROM @table t 
         ) data 
        FOR XML path('tr') 
         ,type 
        ) 
      FOR XML path('table') 
       ,type 
      ) AS NVARCHAR(MAX)) 
+0

Merci. Des idées sur comment je peux changer le style CSS quand les jours sont les mêmes? c'est-à-dire supprimer la bordure pour les colonnes où nous affichons null? – curious