想保留ID,但将每个产品列转为1个产品列。 来自
ID Product 1 Product 2 Product 3
1111 Apple Cherry Peach
2222 Apple
3333 Chery Cherry
收件人
ID Products
1111 Apple
1111 Cherry
1111 Peach
2222
2222
2222 Apple
3333 Chery
3333 Cherry
3333
答案 0 :(得分:0)
使用 union 查询:
Select ID, [Product 1] As Products
From YourTable
Union All
Select ID, [Product 2] As Products
From YourTable
Union All
Select ID, [Product 3] As Products
From YourTable
Order By ID, Products
答案 1 :(得分:0)
使用UNION ALL和每个查询的新列以获取正确的顺序:
select t.ID, t.Products
from (
select 1 AS col, ID, product1 AS Products from tablename
union all
select 2, ID, product2 from tablename
union all
select 3, ID, product3 from tablename
) AS t
ORDER BY t.ID, t.col
结果:
ID Products
1111 Apple
1111 Cherry
1111 Peach
2222
2222
2222 Apple
3333 Cherry
3333 Cherry
3333