在SQL中将行转换为列
我想使用SQL语句转换下表:在SQL中将行转换为列,sql,pivot,Sql,Pivot,我想使用SQL语句转换下表: **Itemcode** | **Number** | **Description** 001 | 1 | blue 001 | 2 | M 002 | 1 | yellow 002 | 3 | Nike 003 | 1 | blue 003
**Itemcode** | **Number** | **Description**
001 | 1 | blue
001 | 2 | M
002 | 1 | yellow
002 | 3 | Nike
003 | 1 | blue
003 | 2 | L
003 | 3 | Adidas
进入
我尝试在我的语句中使用Pivot,但没有成功。谁能帮我?你可以使用聚合:
select code,
max(case when number = 1 then Description end) as one,
max(case when number = 2 then Description end) as two,
max(case when number = 3 then Description end) as three
from table t
group by code;
有很多操作系统已经回答了与此相同的问题。可能是重复的。您可以将您的pivot语句添加到您的问题中吗?您可以发布您的pivot语句吗?
select code,
max(case when number = 1 then Description end) as one,
max(case when number = 2 then Description end) as two,
max(case when number = 3 then Description end) as three
from table t
group by code;