按顺序连接多个值

问题描述 投票:0回答:1

我在 SQL Server 2008 中有一个疑问如何用逗号分隔(,)连接具有列顺序的多个列

时间安排

Id |  Name | days |daysfrequency|scheduletime
1  |  a    |Day4  |Monthly      |22:10
1  |  a    |Thu   |Weekly       |07:30
1  |  a    |Fri   |Daily        |23:10
2  |  b    |Mon   |Weekly        |20:00
2  |  b    |Tue   |Weekly        |23:10
2  |  b    |Wed   |Weekly        |18:10
2  |  b    |Thu   |Weekly        |10:23
2  |  b    |Fri   |Weekly        |1:23

根据上述数据,我想要如下输出。

id | Name  | Days              |DaysFrequency        | ScheduleTIme
1  |a      |Days4,THu,Fri      |Monthly,Weekly,Daily |22:10,07:30,23:10
2  |b      |Mon,Tue,Wed,Thu,Fri|Weekly               |20:00,23:10,18:10,10:23,1:23

我已经尝试过如下。

select id ,name 
,Days=stuff((select distinct ',' days from timeschedule t
 where t.id=t1.id and t.name=t1.name
for xml path('')),1,1,'')

,daysfrequency=stuff((select distinct ',' daysfrequency from timeschedule t
 where t.id=t1.id and t.name=t1.name
for xml path('')),1,1,'')

,scheduletime=stuff((select distinct ',' scheduletime from timeschedule t
 where t.id=t1.id and t.name=t1.name
for xml path('')),1,1,'')
from timeschedule t1 group by id,name

使用上面的查询,它没有给出days、daysfrequenc、scheduletime中的预期顺序。

有人可以告诉我如何在 SQL Server 2008 版本中编写查询来完成此任务吗?

sql sql-server sql-server-2008
1个回答
0
投票

请尝试以下解决方案。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (id INT, _name CHAR(1), days VARCHAR(10), daysfrequency VARCHAR(10), scheduletime VARCHAR(10));
INSERT INTO @tbl (id,_name,days,daysfrequency,scheduletime) VALUES
(1, 'a', 'Day4','Monthly','22:10'),
(1, 'a', 'Thu', 'Weekly', '07:30'),
(1, 'a', 'Fri', 'Daily ', '23:10'),
(2, 'b', 'Mon', 'Weekly', '20:00'),
(2, 'b', 'Tue', 'Weekly', '23:10'),
(2, 'b', 'Wed', 'Weekly', '18:10'),
(2, 'b', 'Thu', 'Weekly', '10:23'),
(2, 'b', 'Fri', 'Weekly', '1:23');
-- DDL and sample data population, end

SELECT id, _name 
    , Days=STUFF((SELECT DISTINCT ',' + days 
    FROM @tbl t
    WHERE t.id=t1.id and t._name=t1._name
    FOR xml path('')),1,1,'')
    , daysfrequency=STUFF((SELECT DISTINCT ',' + daysfrequency 
    FROM @tbl AS t
    WHERE t.id=t1.id and t._name=t1._name
    FOR XML PATH('')),1,1,'')
    , scheduletime=STUFF((SELECT DISTINCT ',' + scheduletime 
    FROM @tbl AS t
    WHERE t.id=t1.id and t._name=t1._name
    FOR XML PATH('')),1,1,'')
FROM @tbl AS t1 
GROUP BY id,_name;

输出

id _名字 天频 安排时间
1 a 第四天,周五,周四 每日、每月、每周 07:30,22:10,23:10
2 b 周五、周一、周四、周二、周三 每周 1:23,10:23,18:10,20:00,23:10
© www.soinside.com 2019 - 2024. All rights reserved.