0

Situation: This table holds the relation information between a Documents table and an Users table. Certain Users need to review or approve documents (Type). I would like to have it to where I could get all of the reviewers on one line if needed. So if three users review Document 1, then a row would have 346, 394, 519 as the value, since those are the reviewers

Table: xDocumentsUsers

DocID..UserID....Type...
1........386......approver
1........346......reviewer
1........394......reviewer..
1........519......reviewer..
4........408......reviewer..
5........408......reviewer..
6........408......reviewer..
7........386......approver..
7........111......readdone..
7........346......reviewer..
8........386......approver..
8........346......reviewer..
9........386......approver..
9........346......reviewer..
10.......386......approver..
11.......386......approver..
11......346......reviewer..
12......386......approver..
12......346......reviewer..
13......386......approver..
13......346......reviewer..
14......386......approver..
14......346......reviewer..
15......386......approver

So desired result would be...

DocID..UserID................Type...

1........386....................approver
1........346,394,519......reviewer.
4........408....................reviewer..
5........408....................reviewer..
6........408....................reviewer..
7........386....................approver..
7........111....................readdone..
7........346....................reviewer..
8........386....................approver..
8........346....................reviewer..
9........386....................approver..
9........346....................reviewer..
10......386....................approver..
11......386....................approver..
11......346....................reviewer..
12......386....................approver..
12......346....................reviewer..
13......386....................approver..
13......346....................reviewer..
14......386....................approver..
14......346....................reviewer..
15......386....................approver
1
  • DECLARE @UsersID varchar(100) SELECT @UsersID = COALESCE (@UsersID + ', ', '') + CAST(UserID AS varchar(5)) FROM xDocumentsUsers WHERE DocumentID = 1 SELECT @UsersID .............................................Gets me this result: 386, 346, 394, 519 Commented Nov 29, 2010 at 19:57

3 Answers 3

2

The FOR XML PATH is a great solution. You need to be aware, though, that it will convert any special characters in the inner SELECTs result set into their xml equivalent - i.e., & will become & in the XML result set. You can easily revert back to the original character by using the REPLACE function around the inner result set. To borrow from astander's previous example, it would look like (note that the SELECT as the 1st argument to the REPLACE function is enclosed in ():

--Concat
SELECT  t.ID,
    REPLACE((SELECT  tIn.Val + ','
        FROM    @Table tIn
        WHERE   tIn.ID = t.ID
        FOR XML PATH('')), '&', '&'))
FROM    @Table t
GROUP BY t.ID
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at

Emulating MySQL’s GROUP_CONCAT() Function in SQL Server 2005

Is there a way to create a SQL Server function to “join” multiple rows from a subquery into a single delimited field?

A simple example is

DECLARE @Table TABLE(
        ID INT,
        Val VARCHAR(50)
)
INSERT INTO @Table (ID,Val) SELECT 1, 'A'
INSERT INTO @Table (ID,Val) SELECT 1, 'B'
INSERT INTO @Table (ID,Val) SELECT 1, 'C'
INSERT INTO @Table (ID,Val) SELECT 2, 'B'
INSERT INTO @Table (ID,Val) SELECT 2, 'C'

--Concat
SELECT  t.ID,
        (
            SELECT  tIn.Val + ','
            FROM    @Table tIn
            WHERE   tIn.ID = t.ID
            FOR XML PATH('')
        )
FROM    @Table t
GROUP BY t.ID

2 Comments

I like the way this example works, if the table exists already would that mean I could just use what is after --Concat ?
Yes, that is true. This is just an example, using a table variable.
0

Does this help?

SELECT DocID
     , [Type]
     , (SELECT CAST(UserID + ', ' AS VARCHAR(MAX)) 
         FROM [xDocumentsUsers] 
         WHERE (UserID = x1.UserID) 
         FOR XML PATH ('')
      ) AS [UserIDs]
FROM [xDocumentsUsers] AS x1

1 Comment

--------------------------- Microsoft SQL Server Management Studio --------------------------- SQL Execution Error. Executed SQL statement: SELECT DocumentID, [Type], (SELECT CAST(UserID + ', ' AS VARCHAR(MAX)) FROM [xDocumentsUsers] WHERE (UserID = x1.UserID) FOR XML PATH('')) AS [UserIDs] FROM [xDocumentsUsers] AS x1 Error Source: .Net SqlClient Data Provider Error Message: Conversion failed when converting the varchar value ', ' to data type int. --------------------------- OK Help ---------------------------

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.