3

Possible Duplicate:
Concatenate row values T-SQL

I have a table like this:

ref_num   name      type       route
----------------------------------------
1         A         W          401
2         B         X          401
3         C         E          401
3         C         E          411
4         D         Z          401
5         K         W          701
5         K         W          991
5         K         W          556
5         K         W          401
6         L         X          401
7         D         Y          401
7         D         Y          411
7         D         Y          680
8         E         Z          401

And i want a RESULT like this:

ref_num   name      type       routes
----------------------------------------
1         A         W          401
2         B         X          401
3         C         E          401,411
4         D         Z          401
5         K         W          701,991,556,401
6         L         X          401
7         D         Y          401,411,680
8         E         Z          401

If anybody can give me a query example to get the desired result it would be greatly appreciated

Community
  • 1
  • 1
RasterOp
  • 37
  • 1
  • 6
  • See: http://stackoverflow.com/questions/122942/how-to-return-multiple-values-in-one-column-t-sql, or This: http://ray.jez.net/concatenation-with-coalesce/ – David Manheim Jun 27 '12 at 14:59

3 Answers3

3
select ref_num, name, type, group_concat(routes)
from your_table
group by ref_num

In MySQL you don't have to group by all values you are selecting. Other values will probably selected by random. But if the other values are the same in one group then you don't have to worry. That is the case in the OP's question.

juergen d
  • 201,996
  • 37
  • 293
  • 362
3

You can Concatenate them with GROUP_CONCAT with GROUP BY, something like:

SELECT ref_num, name, type, GROUP_CONCAT(routes SEPARATOR ', ')   
FROM TableNAme 
GROUP BY ref_num, name, type
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
0

I think WM_CONCAT will do this:

SELECT REF_NUM, NAME, TYPE, WM_CONCAT(ROUTE) 
FROM ROUTES
GROUP BY REF_NUM, NAME, TYPE;
PaddyC
  • 576
  • 7
  • 14