Aze*_*eem 0 sql sql-server-2008-r2
我正在使用以下查询来提取约会数据:
SELECT app.subject, AL.locationName
FROM FilteredAppointment app
INNER JOIN appointmentlocation AL ON app.activityid = AL.appointment
WHERE app.scheduledstart='2013-07-06 15:00:00.000'
Run Code Online (Sandbox Code Playgroud)
输出如下,有2行(具有两个不同位置的相同约会):

如何修改此查询以仅显示一行,其中两个位置与逗号连接,如下所示:
Column1:(MZN; OTV)*...
Column2:Room1,Room2
谢谢
你需要的是SQL Join和连接行,关于它有很多问题.在SQL Server中没有简单的方法可以做到这一点,但这里有一些技巧:
通过使用select for xml进行连接
select
app.subject,
stuff(
(
select ', ' + AL.locationName
from appointmentlocation as AL
where AL.appointment = app.activityid
for xml path(''), type
).value('.', 'nvarchar(max)')
, 1, 2, '')
from FilteredAppointment as app
where app.scheduledstart='2013-07-06 15:00:00.000'
Run Code Online (Sandbox Code Playgroud)
如果您只有一条来自FilteredAppointment的记录要连接,您可以使用聚合到变量:
declare @locations nvarchar(max), @activityid int
select @activityid = ???
select @locations = isnull(@locations + ', ', '') + AL.locationName
from appointmentlocation as AL
where AL.appointment = @activityid
print @locations
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12886 次 |
| 最近记录: |