0

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 enter image description here

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.

0

3 Answers 3

1

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    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that is correct! I'll select your answer in 6 minutes
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.