question

bibinjose83 avatar image
bibinjose83 asked

How do I write sql to get the grouped, concatenated output below?

Table that contains Patient IDs and ICD_codes, but I don’t know how many ICD codes each patient has. As a result, I want to flatten this table, how would you do it? EXAMPLE: Patient_ID ICD_CODES Patient1 code1 Patient1 code2 Patient2 code3 Patient3 code4 Result should look like: Patient1 Code1,Code2 Patient2 Code3 Patient3 Code4
sqlgroup-byconcatenationfor-xml-path
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Do you need to see all the code or is the sum of codes enough? SELECT Patient_ID, count(*) FROM YourTable GROUP BY Patient_ID.
0 Likes 0 ·

1 Answer

· Write an Answer
ThomasRushton avatar image
ThomasRushton answered
I blogged a description of one solution to this process a few years ago. [String Concatenation using XML Path][1]. [1]: https://thelonedba.wordpress.com/2011/02/22/string-concatenation-using-xml-path/
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.