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
 Transact-SQL (2000)
 How to merge similar rows into one??

Author  Topic 

deadfish
Starting Member

38 Posts

Posted - 2004-02-12 : 03:01:55
Hi,

Suppose I have a table table like this:

order_no item_no
-----------------
ord1 item1
ord1 item2
ord1 item3
ord2 item4
ord2 item5

I need to have a result like this:

order_no item_no
------------------------------
ord1 item1,item2,item3
ord2 item4,item5

I have to merge similar rows into 1 row.
Can I achieve this without using a cursor??

Thanks for any help here!

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2004-02-12 : 03:33:23
For SQL 7 click here: http://www.sqlteam.com/item.asp?ItemID=11021
SQL 2000: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=19647

OS
Go to Top of Page

PieterK
Starting Member

2 Posts

Posted - 2004-03-05 : 05:58:45
quote:
Originally posted by deadfish

Hi,

Suppose I have a table table like this:

order_no item_no
-----------------
ord1 item1
ord1 item2
ord1 item3
ord2 item4
ord2 item5

I need to have a result like this:

order_no item_no
------------------------------
ord1 item1,item2,item3
ord2 item4,item5

I have to merge similar rows into 1 row.
Can I achieve this without using a cursor??

Thanks for any help here!


Go to Top of Page

PieterK
Starting Member

2 Posts

Posted - 2004-03-05 : 06:06:50
quote:
Originally posted by deadfish

Hi,

Suppose I have a table table like this:

order_no item_no
-----------------
ord1 item1
ord1 item2
ord1 item3
ord2 item4
ord2 item5

I need to have a result like this:

order_no item_no
------------------------------
ord1 item1,item2,item3
ord2 item4,item5

I have to merge similar rows into 1 row.
Can I achieve this without using a cursor??

Thanks for any help here!




Hi, Sorry for the first reply that was empty!

I Tested on SQL 2000
There should be a Case scenario for each Item

SELECT order_no,
(SELECT ANYNAME = CASE item_no WHEN 'item_1' THEN item_no ELSE 'NOTHING' END) AS ITEM_1,
(SELECT ANYNAME = CASE item_no WHEN 'item_2' THEN item_no ELSE 'NOTHING' END) AS ITEM_2

FROM YOUR_TABLENAME
Go to Top of Page
   

- Advertisement -