我正在制作一份报告,展示他们参与的学生和活动。
报告请求每行需要一名学生。活动表每行有一个活动。我想做一个支点,但找不到办法。我没有知道可以使用的列,并且有超过五种活动类型。
对此的任何帮助将不胜感激。
示例表:
aaron football
aaron soccer
aaron baseball
aaron tennis
aaron basketball
aaron volleyball
steve basketball
steve swimming
示例输出:
aaron football soccer baseball tennis basketball
steve basketball swimming
编辑:使用Oracle 11g
答案 0 :(得分:2)
您必须为每项运动指定排名,然后应用PIVOT。
Oracle 11g R2架构设置:
create table myt(
player varchar2(10),
sport varchar(20)
);
insert into myt values('aaron', 'football');
insert into myt values('aaron', 'soccer');
insert into myt values('aaron', 'baseball');
insert into myt values('aaron', 'tennis');
insert into myt values('aaron', 'basketball');
insert into myt values('aaron', 'volleyball');
insert into myt values('steve', 'basketball');
insert into myt values('steve', 'swimming');
with x(player, sport, r) as (
select player, sport, row_number() over (partition by player order by null) r
from myt
)
select * from x
pivot(max(sport) for r in (
1 as first,
2 as second,
3 as third,
4 as fourth,
5 as fifth
)
)
<强> Results 强>:
| PLAYER | FIRST | SECOND | THIRD | FOURTH | FIFTH |
|--------|------------|----------|----------|--------|------------|
| aaron | football | soccer | baseball | tennis | volleyball |
| steve | basketball | swimming | (null) | (null) | (null) |