- May 27, 2002
- 12,648
- 201
- 106
I have 2 tables as follows
[N_ID], [Name]
1, Scott
2, Wendy
3, Bill
[C_ID], [N_ID], [COLOR]
1, 1, BLUE
2, 1, RED
3, 1, YELLOW
4, 2, Orange
5, 2, Green
6, 3, Purple
7, 3, Pink
i need to write a query which will flatten table2 records (and concatenate them) in a join with table 1, so the output is like this:
[N_ID], [Name], [Colors]
1, Scott, BLUE+RED+YELLOW
2, Wendy, ORANGE+GREEN
3, BILL, Purple+Pink
and 1 limitation, I can't use a CTE.
I'm guessing I need some sort of outer/inner query or a while loop.
Select N_ID, Name,
( Select COLOR from T2 where T2.N_ID = T1.N_ID) AS COLOR
FROM T1
But how to I get the inner query to concatenate all into 1 field?
[N_ID], [Name]
1, Scott
2, Wendy
3, Bill
[C_ID], [N_ID], [COLOR]
1, 1, BLUE
2, 1, RED
3, 1, YELLOW
4, 2, Orange
5, 2, Green
6, 3, Purple
7, 3, Pink
i need to write a query which will flatten table2 records (and concatenate them) in a join with table 1, so the output is like this:
[N_ID], [Name], [Colors]
1, Scott, BLUE+RED+YELLOW
2, Wendy, ORANGE+GREEN
3, BILL, Purple+Pink
and 1 limitation, I can't use a CTE.
I'm guessing I need some sort of outer/inner query or a while loop.
Select N_ID, Name,
( Select COLOR from T2 where T2.N_ID = T1.N_ID) AS COLOR
FROM T1
But how to I get the inner query to concatenate all into 1 field?