SQL 2012 html table 具有 xml 具有非重复列值的路径

SQL 2012 html table with xml path with nonrepeating column values

有了这个table:

  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)

这是某些列出每天和每小时值的查询的结果。我正在使用我在某处找到的以下代码(我使用它但我并不真正理解它)从 @table 生成 html 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))

这给了我一个 html table 这样的:

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

我要的是这样的table:

Day         Hour    Val

2016-06-15  8       1
            9       1
            10      7
2016-06-16  8       3
            9       3
           10       5

即不要在几天内重复相同的值,只在新的一天显示日期值。

使用Row_Number() 函数显示您的预期结果。像这样尝试,

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))