3

First of all, I've looked at this, and my question is different - albeit slightly. Also I have tried the approach and tried to modify it to work for me, but no dice.

Question:

From a result of a couple of queries I get the following result:

ID   |  NAME  |   DESCID  |   TALL   |   GOODLOOKING   |   FAT
_______________________________________________________________
1    |  John  |      1    |  '1.8m'  |       Null      |   Null
1    |  John  |      2    |   Null   |      'Yes'      |   Null
1    |  John  |      3    |   Null   |       Null      |  '84kg'
1    |  John  |      4    |   Null   |       Null      |  '85kg'

Note: Just some dummy data BTW.

I need the output to be the following:

ID   |  NAME  |   TALL   |   GOODLOOKING   |       FAT
__________________________________________________________
1    |  John  |  '1.8m'  |       'Yes'     |   '84kg|85kg'

If this is not possible I would appreciate knowing that, so feel free to tell me.

Any help SQL legends?

5
  • 1
    so only concatenation in fat column? what if Goodlooking had 2 not null distinct values? Commented Jun 17, 2015 at 10:43
  • Concatenation in any of the columns following descid. Thanks for asking Commented Jun 17, 2015 at 10:45
  • Group_concat, is that SQL Server, or just MySQL? Commented Jun 17, 2015 at 10:53
  • Sql server. As in tag. Commented Jun 17, 2015 at 10:54
  • You could also take a look at recursive CTEs Commented Jun 17, 2015 at 11:02

1 Answer 1

3

There is nothing like Group_Concat in SQL Server which can be used directly. You can use FOR XML in a correlated query

SELECT ID,NAME,
STUFF((SELECT '|'+TALL        FROM Tbl1 t2 WHERE t2.ID = t1.ID AND t2.NAME = t1.NAME ORDER BY t2.descid FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,1,'') as tall,
STUFF((SELECT '|'+GOODLOOKING FROM Tbl1 t2 WHERE t2.ID = t1.ID AND t2.NAME = t1.NAME ORDER BY t2.descid FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,1,'') as GOODLOOKING,
STUFF((SELECT '|'+FAT         FROM Tbl1 t2 WHERE t2.ID = t1.ID AND t2.NAME = t1.NAME ORDER BY t2.descid FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)'),1,1,'') as FAT
FROM Tbl1 t1
GROUP BY ID,NAME

SQL Fiddle

Sign up to request clarification or add additional context in comments.

4 Comments

Looks good. I've upvoted for effort, will mark as answer after test. Thanks
Okay well This works. The solution however is too slow. Is that just a product of this type of work? Because this is slower than brunt looping in c# - considerably.
Yes, This would be slow as sql needs to read the table 4 times.
@Terrance00: Since you are saying that the non-aggregated row set is the result of a couple of queries, you might try and see if the technique suggested here can be combined with any of those queries – who knows, it might perform better when applied to original datasets.

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.