這裏使用的一種方式XML PATH
招
;with cte as
(
SELECT *
FROM (VALUES ('DN60','LTE','CPU Load',Cast('2/14/2016 7:00' AS DATETIME)),
('DN60','LTE','CPU Load',Cast('2/14/2016 8:00' AS DATETIME)),
('DN60','LTE','CPU Load',Cast('2/14/2016 9:00' AS DATETIME))) cs (NAME, Type, Data, Time)
)
SELECT DISTINCT a.NAME,
a.Type,
a.Data,
LEFT(cs.time, Len(cs.time) - 1) AS time
FROM cte a
CROSS APPLY (SELECT CONVERT(VARCHAR(20), Time, 8) + ','
FROM cte B
WHERE a.NAME = b.NAME
AND a.Type = b.Type
AND a.Data = b.Data
FOR XML PATH('')) cs (time)
結果:
╔══════╦══════╦══════════╦════════════════════════════╗
║ NAME ║ Type ║ Data ║ time ║
╠══════╬══════╬══════════╬════════════════════════════╣
║ DN60 ║ LTE ║ CPU Load ║ 07:00:00,08:00:00,09:00:00 ║
╚══════╩══════╩══════════╩════════════════════════════╝
檢查了這一點:HTTP: //stackoverflow.com/questions/194852/concat enate-many-rows-into-a-single-text-string 您需要先修剪'Time'字段的日期 – Nicarus