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 2005 Forums
 Transact-SQL (2005)
 Creating a List

Author  Topic 

sross81
Posting Yak Master

228 Posts

Posted - 2011-06-24 : 12:16:56
Hello,
I have been searching for a way to make this query work. I thought Coalesce would work, but from what I read it doesn't seem to be what I am looking for. I have done case statements and concatenated multiple fields into one list, but I have not found a way to do it with the same field. I have this query.

select distinct a.person_id,c.brand_name
from person a
left outer join patient_medication b on a.person_id = b.person_id
left outer join fdb_medication c on b.ndc_id = c.ndc_id
where c.brand_name IN('Lescol','Lipitor','Torvast','Zocor','Lipex','Pravachol','Selektine','Lipostat','Crestor','Altocor','Mevacor')
and (b.date_stopped is null or b.date_stopped = '')


My results are
person id, brand_name
1, Lescol
1, Lipitor

I want them to be
person_id, brand_name
1, (Lescol, Lipitor)

Is that possible?

Thanks in Advance!
Sherri

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-24 : 12:29:11
see
http://www.nigelrivett.net/SQLTsql/CSVStringSQL.html

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sross81
Posting Yak Master

228 Posts

Posted - 2011-06-24 : 13:59:50
This site is alot easier to understand and follow. I just thought I would share in case anyone else needs help. Thanks for giving me an idea of what to search on.

[url]http://sqlwithmanoj.wordpress.com/2010/09/16/combine-multiple-rows-to-csv-string-and-vice-versa/[/url]

quote:
Originally posted by nigelrivett

see
http://www.nigelrivett.net/SQLTsql/CSVStringSQL.html

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Thanks in Advance!
Sherri
Go to Top of Page
   

- Advertisement -