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
 General SQL Server Forums
 New to SQL Server Programming
 Combine results into one row

Author  Topic 

nstrong
Starting Member

2 Posts

Posted - 2009-02-24 : 16:13:53
Hello all! ^^

So I've been fuddling with a query in MySQL for several hours, and I've gotten as far as I can, and could use some help.

My view returns a list of sales orders and tracking numbers like so:


id sales_order tracking_number
10059 58322 z003001
10060 58323 z003002
10061 58323 z003003
10062 58324 z003004
10063 58324 z003005


I would like the view to return this instead though:


id sales_order tracking_number
10059 58322 z003001
10060 58323 z003002, z003003
10062 58324 z003004, z003005


Googling for a solution is burning my eyes out... does anyone here perhaps know how to combine these records?

Thank you!

~ Nick

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-24 : 16:18:14
Is this SQL Server or MYSQL question? If you are using SQL Server(Which Version)?
Go to Top of Page

nstrong
Starting Member

2 Posts

Posted - 2009-02-24 : 16:21:49
It's a MySQL server.

Wait, is this a Microsoft-only forum??? :-o

Well, if the MS solution doesn't use any crazy coding, I think I could get it to work in MySQL. :-s

~ Nick
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-02-24 : 16:57:46
This might work in MYSQL.

Declare @T table
(id int,sales_order int,tracking_number varchar(20))

Insert @T

Select 1, 58322 , 'z003001' union all
Select 2,58323 , 'z003002' union all
Select 3, 58323 , 'z003003' union all
Select 4,58324 , 'z003004' union all
Select 5,58324 , 'z003005'

Select Min(id)as id,sales_order,
Max(Case When seq =1 then tracking_number Else '' End) + ',' +
Max(Case When seq =2 then tracking_number Else '' End) + ',' +
Max(Case When seq =3 then tracking_number Else '' End) + ',' +
Max(Case When seq =4 then tracking_number Else '' End)as tracking_number
from
(Select s.id,s.sales_order,tracking_number,
(Select Count(*) from @T Where sales_order =s.sales_order
and tracking_number <=s.tracking_number)as seq
from @T s)M
Group by sales_order
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-25 : 02:53:05
quote:
Originally posted by nstrong

It's a MySQL server.

Wait, is this a Microsoft-only forum??? :-o

Well, if the MS solution doesn't use any crazy coding, I think I could get it to work in MySQL. :-s

~ Nick


Make use of Group_concat function

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -