0
点赞
收藏
分享

微信扫一扫

从头开始学Oracle--------行转列

Just_Esme 2022-07-27 阅读 64
SQL数据库


 

with temp as(
select '大宇' name ,'语文' subject,'85' score from dual union all
select '大宇' name ,'数学' subject,'81' score from dual union all
select '小宇' name ,'语文' subject,'77' score from dual union all
select '小宇' name ,'数学' subject,'72' score from dual union all
select '小大宇' name ,'语文' subject,'58' score from dual
)
select name,
-- 如果subject是语文的话,这一列取score,如果score为空则取0
max(decode(subject, '语文', score, '0')) as SCORE_语文,
max(decode(subject, '数学', score, '0')) as SCORE_数学
from temp group by name;

从头开始学Oracle--------行转列_SQL

with temp as(
select '四川省' nation ,'成都市' city,'第一' ranking from dual union all
select '四川省' nation ,'绵阳市' city,'第二' ranking from dual union all
select '四川省' nation ,'德阳市' city,'第三' ranking from dual union all
select '四川省' nation ,'宜宾市' city,'第四' ranking from dual union all
select '湖北省' nation ,'武汉市' city,'第一' ranking from dual union all
select '湖北省' nation ,'宜昌市' city,'第二' ranking from dual union all
select '湖北省' nation ,'襄阳市' city,'第三' ranking from dual
)
select nation,
max(decode(ranking, '第一', city, '')) as 第一,
max(decode(ranking, '第二', city, '')) as 第二,
max(decode(ranking, '第三', city, '')) as 第三,
max(decode(ranking, '第四', city, '')) as 第四
from temp group by nation;

 

举报

相关推荐

0 条评论