Note that there are some explanatory texts on larger screens.

plurals
  1. POConcatenate row values T-SQL
    primarykey
    data
    text
    <p>I am trying to pull together some data for a report and need to concatenate the row values of one of the tables. Here is the basic table structure:</p> <p><strong>Reviews</strong> </p> <pre><code> ReviewID ReviewDate </code></pre> <p><strong>Reviewers</strong> </p> <pre><code> ReviewerID ReviewID UserID </code></pre> <p><strong>Users</strong> </p> <pre><code>UserID FName LName </code></pre> <p>This is a M:M relationship. Each Review can have many Reviewers; each User can be associated with many Reviews.</p> <p>Basically, all I want to see is Reviews.ReviewID, Reviews.ReviewDate, and a concatenated string of the FName's of all the associated Users for that Review (comma delimited).</p> <p>Instead of:</p> <pre><code>ReviewID---ReviewDate---User 1----------12/1/2009----Bob 1----------12/1/2009----Joe 1----------12/1/2009----Frank 2----------12/9/2009----Sue 2----------12/9/2009----Alice </code></pre> <p>Display this:</p> <pre><code>ReviewID---ReviewDate----Users 1----------12/1/2009-----Bob, Joe, Frank 2----------12/9/2009-----Sue, Alice </code></pre> <p>I have found <a href="http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/" rel="noreferrer" title="this">this</a> article describing some ways to do this, but most of these seem to only deal with one table, not multiple; unfortunately, my SQL-fu is not strong enough to adapt these to my circumstances. I am particularly interested in the example on that site which utilizes FOR XML PATH() as that looks the cleanest and most straight forward.</p> <pre><code>SELECT p1.CategoryId, ( SELECT ProductName + ', ' FROM Northwind.dbo.Products p2 WHERE p2.CategoryId = p1.CategoryId ORDER BY ProductName FOR XML PATH('') ) AS Products FROM Northwind.dbo.Products p1 GROUP BY CategoryId; </code></pre> <p>Can anyone give me a hand with this? Any help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload