-1

想保留 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
4

2 回答 2

0

使用联合查询:

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

于 2019-08-15T14:25:20.093 回答
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    
于 2019-08-15T14:30:35.963 回答