0

I have these 3 tables as below.

 Table: Student ------------- | ID | Name| +-----+-----+ | S01 |Alex | | S02 |Sam | | S03 |May | ------------- Table: Book -------------- | ID | Name | +-----+------+ | B01 |BookA | | B02 |BookB | | B03 |BookC | -------------- Table:StudentBooks -------------- | SID| BID | +-----+------+ | S01 |B01 | | S02 |B02 | | S01 |B03 | | S02 |B03 | -------------- 

Here is the output I want to get.

 ----------------------- | Name | Book | +-------+-------------+ | Alex |BookA, Book C| | Sam |BookB, Book C| | May | | ----------------------- 

I tried with the following code, but it seems not able to get the correct book name based on the student name. My current output is that all the books are shown in every row, including student named 'May' which not allocated with any books.

SELECT s.Name AS Name, STUFF((SELECT ',' + b.Name FROM StudentBook sb JOIN Student s ON s.ID = sb.SID JOIN Book b ON b.ID = sb.BID WHERE s.Name = s.Name FOR XML PATH('')),1,1,'') AS Book FROM StudentBooks sb JOIN Book b ON b.ID = sb.BID JOIN Student s ON s.ID = sb.SID; 
1
  • WHERE s.Name = s.Name? When is the value of s.Name not going to be the value of s.Name (apart from when it's NULL)? Commented Apr 8, 2020 at 9:34

2 Answers 2

1

Your inner & outer references are not matching. You are referencing student name to book :

SELECT s.Name AS Name, STUFF((SELECT ',' + b.Name FROM StudentBook sb JOIN Book b ON b.ID = sb.BID WHERE sb.sid = s.id FOR XML PATH('') ), 1, 1, '' ) AS Book FROM Student s; 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried with this but I'm still getting all the books for every students.. Even student name 'May' who is not allocated with any books.
@newlearner. . . This does what you want with May who doesn't have any books.
yes it works! sorry I missed out Where statement. Thank you so much!!
0

STRING_AGG will also solve the problem

SELECT S.[Name], STRING_AGG(B.[Name], ', ') AS Books FROM StudentBooks AS SB WITH(NOLOCK) INNER JOIN Student AS S WITH(NOLOCK) ON S.ID = SB.SID INNER JOIN Book AS B WITH(NOLOCK) ON B.ID = SB.BID GROUP BY S.[Name] 

3 Comments

its having error :'STRING_AGG' is not a recognized built-in function name.
I saw "sql-server" was tagged, is this not what you are using?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.