create table match_results (
match_date date,
location varchar2(20),
home_team_name varchar2(20),
away_team_name varchar2(20),
home_team_points integer,
away_team_points integer
);
insert into match_results values ( date'2018-01-01', 'Snowley', 'Underrated United', 'Terrible Town', 2, 0 );
insert into match_results values ( date'2018-01-01', 'Coldgate', 'Average Athletic', 'Champions City', 1, 4 );
insert into match_results values ( date'2018-02-01', 'Dorwall', 'Terrible Town', 'Average Athletic', 0, 1 );
insert into match_results values ( date'2018-03-01', 'Coldgate', 'Average Athletic', 'Underrated United', 3, 3 );
insert into match_results values ( date'2018-03-02', 'Newdell', 'Champions City', 'Terrible Town', 8, 0 );
commit;
select * from match_results
位置玩了多少场比赛。您可以通过以下方式通过 group 执行此操作
select location, count (*)
from match_results
group by location;
使用 case 表达式执行此操作
select count ( case when location = 'Snowley' then 1 end ) snowley,
count ( case when location = 'Coldgate' then 1 end ) coldgate,
count ( case when location = 'Dorwall' then 1 end ) dorwall,
count ( case when location = 'Newdell' then 1 end ) newdell
from match_results;
pivot 子句执行相同操作
要进入位置列的值。你想要一个计数,所以这是 count(*)
包含要成为新列的值的列。这是位置
要成为列的值列表。这是位置的名称
with rws as (
select location from match_results
)
select * from rws
pivot (
count(*) for location in (
'Snowley', 'Coldgate', 'Dorwall', 'Newdell'
)
);
查看每个月玩的数量,将月份显示为列
with rws as (
select to_char ( match_date, 'MON' ) match_month
from match_results
)
select * from rws
pivot (
count (*) for match_month in (
'JAN', 'FEB', 'MAR'
)
);
使用 where 子句过滤数据透视的输出。
显示每个位置每个月进行的比赛数量的表格
with rws as (
select location, to_char ( match_date, 'MON' ) match_month
from match_results
)
select * from rws
pivot (
count (*) for match_month in (
'JAN', 'FEB', 'MAR'
)
)
with rws as (
select location, to_char ( match_date, 'MON' ) match_month
from match_results
)
select * from rws
pivot (
count (*) for match_month in (
'JAN', 'FEB', 'MAR'
)
)
where "'JAN'" > 0
为列表中的每个值设置别名
with rws as (
select location, to_char ( match_date, 'MON' ) match_month
from match_results
)
select * from rws
pivot (
count (*) for match_month in (
'JAN' jan, 'FEB' feb, 'MAR' mar
)
);
with rws as (
select location, to_char ( match_date, 'MON' ) match_month
from match_results
)
select * from rws
pivot (
count (*) for match_month in (
'JAN' jan, 'FEB' feb, 'MAR' mar
)
)
where jan > 0;
按每个月的列显示
with rws as (
select location, to_char ( match_date, 'MON' ) match_month ,
home_team_points, away_team_points
from match_results
)
select * from rws
pivot (
count (*) matches,
sum ( home_team_points ) home_points,
sum ( away_team_points ) away_points
for match_month in (
'JAN' jan, 'FEB' feb, 'MAR' mar
)
);
这样看的
数数(*)
总和( home_team_points )
总和( away_team_points )
修改成带中国字的
逆向旋转
要获得每场比赛的主客场球队的一行,您需要两个查询
select match_date, location, 'HOME' home_or_away, home_team_name team
from match_results
union all
select match_date, location, 'AWAY' home_or_away, away_team_name team
from match_results
order by match_date, location, home_or_away;
select match_date, location, home_or_away, team
from match_results
unpivot (
team for home_or_away in (
home_team_name as 'HOME', away_team_name as 'AWAY'
)
)
order by match_date, location, home_or_away;
这个太长了,不想了
with rws as (
select home_team_name, away_team_name,
case
when home_team_points > away_team_points then 'WON'
when home_team_points < away_team_points then 'LOST'
else 'DRAW'
end home_team_result,
case
when home_team_points < away_team_points then 'WON'
when home_team_points > away_team_points then 'LOST'
else 'DRAW'
end away_team_result
from match_results
)
select team, w, d, l
from rws
unpivot (
( team, result ) for home_or_away in (
( home_team_name, home_team_result ) as 'HOME',
( away_team_name, away_team_result ) as 'AWAY'
)
)
pivot (
count (*), min ( home_or_away ) dummy
for result in (
'WON' W, 'DRAW' D, 'LOST' L
)
)
order by w desc, d desc, l;