0

I am using following query to extract appointment data:

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'

The output is as follows with 2 rows (same appointment with two different locations):

enter image description here

How can i modify this query to display only one row with two locations concatenated with comma like below:

Column1: (MZN; OTV)*...
Column2: Room1,Room2

Thanks

1
  • it is not duplicate to that question as I needed to populate the column values from different rows and same column. The post your suggested, the values are coming from other columns. Commented Aug 1, 2013 at 11:44

4 Answers 4

5

what you need is SQL Join and concatenate rows, there are many questions on SO about it. There's no easy way to do this in SQL Server, but here some tricks:

Concatenate by using 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'

if you have only one record from FilteredAppointment to concatenate, you could use aggregating into variable:

declare @locations nvarchar(max), @activityid int

select @activityid = ???

select @locations = isnull(@locations + ', ', '') + AL.locationName
from appointmentlocation as AL
where AL.appointment = @activityid

print @locations
Sign up to request clarification or add additional context in comments.

Comments

2

This example will help you .. or wait until I makequery for you

  USE app.subject,
    SELECT      AL.locationName AS [Loc],
                STUFF((    SELECT ',' + SUB.Name AS [text()]
                            – Add a comma (,) before each value
                            FROM appointmentlocation AL
                            WHERE
                            app.activityid = AL.appointment

                            FOR XML PATH('') – Select it as XML
                            ), 1, 1, '' )
                            – This is done to remove the first character (,)
                            – from the result
                AS [Sub Categories]
    FROM  FilteredAppointment app

1 Comment

thanks but i guess answer given by Roman Pekar is working...
0
SELECT app.subject, GROUP_CONCAT(AL.locationName, ', ') AS LocationName
FROM FilteredAppointment app
INNER JOIN appointmentlocation AL ON app.activityid = AL.appointment
WHERE app.scheduledstart='2013-07-06 15:00:00.000'
GROUP BY app.subject

Untested

5 Comments

GROUP_CONCAT is nice, but MySQL specific. The post is tagged MS SQL.
Ah my bad, wasn't aware it was specific to mysql!
@jpw : OP wants in SQL Server.
'GROUP_CONCAT' is not a recognized built-in function name in SQL Server
0

I can't test it right now but I think this might do the work

select subject, 
       group_concat(name separator ',') Newcolumn
  from table
 group by subject

2 Comments

this is for MySQL not for SQL Server
Yes I didn't know that but I gave a workaround for msSQL in @Dale post

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.