Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 merging multiple rows into one

Author  Topic 

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2001-11-29 : 17:23:19
How do I convert multiple rows into a single delimited row for each key value?

Example:
key value
1 green
1 blue
1 brown
2 purple
2 yellow

becomes...

key value
1 green, blue, brown
2 purple, yellow

I think I've seen it discussed before but I can't come up with the right terms to find it.

Thanks

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2001-11-29 : 17:27:45
Hi

There are a few ways thrown around here that will do what you want

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9978



Damian
Go to Top of Page

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2001-11-30 : 14:10:58
Thanks - that helps. I'm a little stuck though trying to make it work.

I'm trying to build a string of patient allergies for a given patient. There are 3 parts - the date, the allergy name, and the reaction. The reaction may be null for any given allergy so I have to give it some sort of value or the coalesce won't work.

Here's my code trying to use the CASE statement. This has syntax errors in the 'RXN' = Case line.

Anybody know how to make this work?

 
SET NOCOUNT ON

INSERT INTO TBL_PATIENT_ALLERGIES_SUMMARY (PATIENT_CDR_ID, username)
SELECT PATIENT_CDR_ID, username FROM TBL_PATIENT_ALLERGIES
GROUP BY PATIENT_CDR_ID, username

DECLARE @allergies varchar(7000)
DECLARE @iRow int
DECLARE @iMax int
SELECT @iMax = count(*) from TBL_PATIENT_ALLERGIES_SUMMARY
SELECT @iRow = 1
While @iRow < @iMax

BEGIN

--build the csv string
SELECT @allergies = null
Select @allergies = Coalesce(@allergies + ', ','')+ CONVERT(varchar(30), ALLERGY_DATE,1) + '-' + ALLERGY_NAME +
'RXN' = CASE
WHEN REACTION IS NOT NULL THEN ' (' + REACTION + ')'
WHEN REACTION IS NULL THEN ''
END

FROM TBL_PATIENT_ALLERGIES
WHERE
PATIENT_CDR_ID = (SELECT PATIENT_CDR_ID FROM TBL_PATIENT_ALLERGIES_SUMMARY WHERE sys_id = @iRow)


--UPDATE THE TABLE
UPDATE TBL_PATIENT_ALLERGIES_SUMMARY
SET allergy_string = @allergies
WHERE sys_id = @iRow

SELECT @iRow = @iRow + 1
END


SELECT * FROM TBL_PATIENT_ALLERGIES_SUMMARY


Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2001-11-30 : 15:53:03
Instead of using a CASE statement in this situation you might try an ISNULL() statement instead.

-------------------
It's a SQL thing...
Go to Top of Page

ToddV
Posting Yak Master

218 Posts

Posted - 2001-11-30 : 16:49:11
Take out 'RXN' = and you sold be ready to fly.

FYI Makes more sense to me Propopulate the Variable with empty string than to use the Coalesce function.


Go to Top of Page

smccreadie
Aged Yak Warrior

505 Posts

Posted - 2001-12-03 : 10:30:12
Ahhh - that's it. Thanks


quote:

Take out 'RXN' = and you sold be ready to fly.

FYI Makes more sense to me Propopulate the Variable with empty string than to use the Coalesce function.






Go to Top of Page
   

- Advertisement -