简体   繁体   中英

SQL query select multiple rows in one field based on id

I have this query:

SELECT Id,
SUBSTRING(
(SELECT ',' + lts.Selection
FROM LiveTrainingSelections lts 
INNER JOIN LiveTraining lt ON lt.Id = lts.LiveTrainingId
WHERE lts.SelectionType = 'Session Format' and lts.LiveTrainingId = lt.Id
FOR XML PATH('')),2,200000) AS SelectionDetails
 FROM LiveTraining lt
JOIN LiveTrainingSelections lts 
ON lt.Id = lts.LiveTrainingId

This is working almost properly, the only thing is that I'm getting all the records concatenated in one field, but I want to get all the fields specific for that Id, please check the screenshot 在此处输入图片说明

As you can see in the first result all the values are concatenated and the LiveTrainingId is ignored, but what I want to get is only the three values specific for that LiveTrainingId concatenated.

Those joins are not right. The alias lt in your subquery is overriding the alias with the same name from the outer FROM clause, which is what you appear to be truly wanting to match to. Renaming the aliases so they are unique - note the 2 's - should work:

SELECT Id,
SUBSTRING(
(SELECT ',' + lts2.Selection
FROM LiveTrainingSelections lts2 
INNER JOIN LiveTraining lt2 ON lt2.Id = lts2.LiveTrainingId
WHERE lts2.SelectionType = 'Session Format' and lts2.LiveTrainingId = lt.Id
FOR XML PATH('')),2,200000) AS SelectionDetails
 FROM LiveTraining lt
JOIN LiveTrainingSelections lts 
ON lt.Id = lts.LiveTrainingId

But from your query I am guessing the joins are actually superfluous and you simply want to select from LiveTraining in the outer query and LiveTrainingSelections in the inner query:

SELECT Id,
SUBSTRING(
(SELECT ',' + lts.Selection
FROM LiveTrainingSelections lts 
WHERE lts.SelectionType = 'Session Format' and lts.LiveTrainingId = lt.Id
FOR XML PATH('')),2,200000) AS SelectionDetails
FROM LiveTraining lt    

I think we can simply do it using STUFF please try, if it gives you the desired output:

SELECT ltd.Id,
STUFF((SELECT ',' + lta.Selection FROM FROM LiveTrainingSelections lta
        WHERE lt.Id = lta.LiveTrainingId 
        AND lta.SelectionType = 'Session Format' 
                                 FOR XML PATH('')),1,1,'') AS SelectionDetails,            
FROM LiveTraining lt
INNER JOIN LiveTrainingSelections lts ON lt.Id = lts.LiveTrainingId
GROUP BY ltd.Id

Please try below sql:

SELECT  It.Id, STUFF((SELECT  ',' + lts.Selection
            FROM LiveTrainingSelections lts 
            WHERE  lts.LiveTrainingId = lt.Id
            ORDER BY lts.LiveTrainingId
        FOR XML PATH('')), 1, 1, '') AS listStr
FROM LiveTraining lt
WHERE exists(
SELECT 1 FROM LiveTrainingSelections lts2
WHERE lt.Id = lts2.LiveTrainingId
and lts2.SelectionType = 'Session Format'
)
GROUP BY lt.Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM