这并不能完全像在 MSSQL 中那样回答问题。我知道有 MySQL 的风格可以做到这一点
declare @tempTable TABLE (dt_timeOfProgram datetime, str_name varchar(10));
insert into @tempTable values
('2003-10-20 17:00', 'Factor'),
('2003-10-21 17:00', 'Factor'),
('2003-10-22 17:00', 'Factor'),
('2003-10-20 19:00', 'Form'),
('2003-10-21 14:00', 'Factor');
select
REPLACE(REPLACE(REPLACE(dow, '</dow><dow>', ','), '<dow>',''),'</dow>', '') as DOW
, [time]
, [showName]
from ( select distinct
CAST(dt_timeOfProgram as time) as [time],
str_name [showName],
( SELECT DATENAME(dw, t1.dt_timeOfProgram) as [dow]
FROM @tempTable t1
where t1.str_name=t2.str_name
and CAST(t1.dt_timeOfProgram as time) = CAST(t2.dt_timeOfProgram as time)
order by t1.dt_timeOfProgram
for XML PATH('')) [dow]
from @tempTable t2) as t
这是结果集
Tuesday 14:00:00.0000000 Factor
Monday,Tuesday,Wednesday 17:00:00.0000000 Factor
Monday 19:00:00.0000000 Form
这是一个相当草率的工作。我永远不会在实时应用程序中这样做。此逻辑将由业务逻辑而不是 db 处理。
数据库资源一般都是 LEASE 可伸缩的。所以最后,您希望尽可能少地使用它们。
祝你好运