views:

37

answers:

1

I have a table CSFT_SuggestionItem with cols like:

SuggestionItemID  Title                Description, etc. 
------------------------------------------------------------
1                 Item 1               Do more work
2                 Item 2               I think more is good

For each SuggestionItem, there can by multiple "Applications" where it can be associated, so I have CSFT_SuggestionItemApplication

SuggestionItemID    ApplicationID
----------------------------------
1                    1
1                    2
2                    1

And I have an CSFT_Application Table

ApplicationID        Description
----------------------------------
1                    Spring
2                    Summer
3                    Fall
4                    Winter

I want my output to look like this:

SuggestionItemID     Applications
----------------------------------
1                    Spring, Summer  
2                    Spring

I know I can do this by Looping. But, is there a better way in Sql server 2005? Maybe COALESCE, PIVOT, etc.

+1  A: 

Use:

SELECT DISTINCT
       sia.SuggestionItemID,
       STUFF((SELECT ','+ a.description
                FROM CSFT_Application a
                JOIN CSFT_SuggestionItemApplication b ON b.applicationid = a.applicationid
               WHERE b.suggestionitemid = sia.suggestionitemid
            GROUP BY a.description
             FOR XML PATH('')), 1, 1, '') AS applications
  FROM CSFT_SuggestionItemApplication sia

Alternate using GROUP BY:

  SELECT sia.SuggestionItemID,
         STUFF((SELECT ','+ a.description
                  FROM CSFT_Application a
                  JOIN CSFT_SuggestionItemApplication b ON b.applicationid = a.applicationid
                 WHERE b.suggestionitemid = sia.suggestionitemid
              GROUP BY a.description
               FOR XML PATH('')), 1, 1, '') AS applications
    FROM CSFT_SuggestionItemApplication sia
GROUP BY sia.SuggestionItemID
OMG Ponies
that doesn't work. It returns 2 rows for SuggestionItemID = 1. Maybe, the group by needs to be different???
Patrick From An IBank
@Patrick From An IBank: It'd return two rows for `SuggestionItemID = 1` if you forgot the `DISTINCT`
OMG Ponies
That didn't work. Here let me provide the real table names:SELECT DISTINCT sia.SuggestionItemID, STUFF((SELECT ','+ a.description FROM CSFT_Application a WHERE a.applicationid = sia.ApplicationID GROUP BY a.description FOR XML PATH('')), 1, 1, '') AS CSFT_Application FROM CSFT_SuggestionItemApplication sia
Patrick From An IBank
done. So any thoughts now?
Patrick From An IBank
OMG Ponies
Your right. I'm not running the what you have. I would get an error "Msg 156, Level 15, State 1, Line 7Incorrect syntax near the keyword 'FOR'." I think it is because there is a comma missing from: SELECT sia.SuggestionItemID,
Patrick From An IBank
@Patrick From An IBank: Thx, yes - my typo on the missing comma, sorry. Try now
OMG Ponies
that didn't work. Sorry, 2 rows back. Any other thoughts?
Patrick From An IBank
@Patrick From An IBank: I can't see how that's possible based on the data provided, but I won't be able to recreate in my environment for testing for an hour or so.
OMG Ponies
it would be appreciated!
Patrick From An IBank
@Patrick From An IBank: My apologies, you were correct that the original query returned duplicate rows with incorrect information. I've tested, and corrected my queries - see the updates. Thanks again for your patience.
OMG Ponies
you the man! Thank you!
Patrick From An IBank