简体   繁体   English

根据带有间隔值的结果在SQL中创建HTML表字符串

[英]Creating an HTML table string in SQL from a result with gap values

I'm using a mailing template engine that can't accept anything other than SQL output (due to this already being a subquery), so I'm trying to output an HTML table directly as a string. 我使用的邮件模板引擎不能接受除SQL输出以外的任何内容(由于该输出已经是子查询),因此我试图将HTML表直接输出为字符串。

I need to create a SQL script that through some form of black magic creates a table out of a set of shows with the days as columns, and the show times as table cells. 我需要创建一个SQL脚本,该脚本通过某种形式的黑魔法从一组显示中创建一个表,其中以天为列,以显示时间为表单元格。 For example: 例如:

FR      SA      SU      MO      TU      WE      TH      FR
16:00   11:10   11:10           16:00   12:50
        13:40   13:40                   15:10
        16:00   16:00

I've got a query that gets all the required data and its column and row numbers, but no idea how to turn that into a string value that looks like this for each row: 我有一个查询,该查询获取所有必需的数据及其列号和行号,但不知道如何将其转换为看起来像每一行的字符串值:

<tr><td>16:00</td><td>11:10</td><td>11:10</td><td></td><td>16:00</td><td>12:50</td><td></td><td></td></tr>

Here's the query (and sample data) I'm using now: 这是我现在正在使用的查询(和示例数据):

declare @Shows table(id int, start datetime);
insert into @Shows(id, start) values (801, '2013-12-13 16:00');
insert into @Shows(id, start) values (325, '2013-12-14 11:10');
insert into @Shows(id, start) values (662, '2013-12-14 13:40');
insert into @Shows(id, start) values (771, '2013-12-14 16:00');
insert into @Shows(id, start) values (772, '2013-12-15 11:10');
insert into @Shows(id, start) values (441, '2013-12-15 13:40');
insert into @Shows(id, start) values (775, '2013-12-15 16:00');
insert into @Shows(id, start) values (138, '2013-12-17 16:00');
insert into @Shows(id, start) values (238, '2013-12-18 12:50');
insert into @Shows(id, start) values (947, '2013-12-18 15:10');


declare @DayWrapHour int=3;
declare @FromDate datetime='2013-12-13 15:00';
declare @ToDate datetime = dateadd(day, 8, CONVERT(date, @FromDate));
set @ToDate = DATEADD(hour, @DayWrapHour, @ToDate); --8 days from now, at 3 am

select *, ROW_NUMBER() over (partition by columnindex order by columnindex) as rownumber
from (
    select
        s.Id,
        CONVERT(char(5), CONVERT(time, s.start)) as StartTime,
        DATEDIFF(DAY,
            DATEADD(HOUR, @DayWrapHour, CONVERT(datetime, CONVERT(date, @FromDate))), --@FromDate, at 3 am
            s.start
        ) as columnindex
    from @Shows s
    where s.start between @FromDate and @ToDate
)as sub
order by rownumber, sub.columnindex

I thought that was quite clever already, but I'm stumped on how to do a sort of foreach on this without using non-SQL code, and on how to deal with the data gaps if I do. 我以为那已经很聪明了,但是我很困惑如何在不使用非SQL代码的情况下对此进行一次foreach,以及如何处理数据空白。

Output of that query: 该查询的输出:

Id  StartTime   columnindex rownumber
801 16:00       0           1
325 11:10       1           1
772 11:10       2           1
138 16:00       4           1
238 12:50       5           1
662 13:40       1           2
441 13:40       2           2
947 15:10       5           2
771 16:00       1           3
775 16:00       2           3

Note that the column amount is fixed, but the row amount is not. 请注意,列数是固定的,但行数不是固定的。

You can use something like this to pivot the columns: 您可以使用类似以下的方法来旋转列:

select 
    rownumber
,   col0 = max(case when columnindex = 0 then StartTime end)
,   col1 = max(case when columnindex = 1 then StartTime end)
,   col2 = max(case when columnindex = 2 then StartTime end)
from (
   -- your previous query with the rownumber and columnindex
) x
group by rownumber

And then for xml to generate the html, like they do in this answer: Create HTML Table with SQL FOR XML 然后for xml生成html,就像在此答案中一样: 使用SQL FOR XML创建HTML表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM