如何在旧版本的SQL Server中模拟string_agg函数

时间:2019-06-13 07:03:43

标签: sql sql-server-2012 group-concat string-aggregation

我想按表按某些列分组并显示其他列的值。在SQL v.2017中,我可以使用string_agg函数来做到这一点。但是我的SQL版本是2012。我该怎么做...

create table dbo.TaskPeriods
(
 [id] [int] NULL,
 [startDate] [datetime] NULL,
 [endDate] [datetime] NULL
 )
 -------------------------------
insert into test.dbo.TaskPeriods values(1,'2018-07-24 00:00:00.000','2018-07-24 10:00:00.000')
insert into test.dbo.TaskPeriods values(2,'2018-07-24 00:00:00.000','2018-07-24 10:00:00.000')
insert into test.dbo.TaskPeriods values(3,'2018-07-24 08:00:00.000','2018-07-24 12:00:00.000')
 --------------------------------------------------------
select id,startDate,endDate from  test.dbo.TaskPeriods
 --------------------------------------------------------
select 
min (startDate)
,max (endDate)
--,string_agg(id,',')  //How to Do this Without string_agg Function
FROM test.dbo.TaskPeriods
group by startDate,endDate

0 个答案:

没有答案