求助各位大神,怎么把列数据换成行数据呢?

mv110335 2018-08-12 04:24:19


怎么样才能处理这样的结果呢?
...全文
320 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
碧水幽幽泉 2018-08-13
  • 打赏
  • 举报
回复
以下SQL可以实现你的需求:

create table tmp as
select '2017' c1, 1 c2,3100 c3 from dual union all
select '2017' c1, 2 c2,3200 c3 from dual union all
select '2017' c1, 3 c2,3300 c3 from dual union all
select '2017' c1, 4 c2,3400 c3 from dual union all
select '2018' c1, 1 c2,4100 c3 from dual union all
select '2018' c1, 2 c2,4200 c3 from dual union all
select '2018' c1, 3 c2,4300 c3 from dual union all
select '2018' c1, 4 c2,4400 c3 from dual;

select c1 as "年",
max(decode(c2,1,c3,null )) as "一月",
max(decode(c2,2,c3,null )) as "二月",
max(decode(c2,3,c3,null )) as "三月",
max(decode(c2,4,c3,null )) as "四月"
from tmp
group by c1
DB丨JOK 2018-08-13
  • 打赏
  • 举报
回复
自行百度 WM_CONCAT 函数
  • 打赏
  • 举报
回复
百度一下 行转列 很多的
qq_32711805 2018-08-13
  • 打赏
  • 举报
回复
mysql> select * from score;
+----+------+------+-------+-------+
| id | name | year | month | score |
+----+------+------+-------+-------+
| 1 | 张三 | 2017 | 2 | 36 |
| 2 | 张三 | 2017 | 3 | 98 |
| 3 | 张三 | 2017 | 4 | 22 |
| 4 | 张三 | 2018 | 1 | 40 |
| 5 | 张三 | 2018 | 2 | 60 |
| 6 | 张三 | 2018 | 3 | 50 |
| 7 | 张三 | 2018 | 4 | 80 |
| 8 | 张三 | 2017 | 1 | 25 |
+----+------+------+-------+-------+


---------------------------------------------------

mysql> select name,
-> year,
-> max(case month when '1' then score else 0 end) '1',
-> max(case month when '2' then score else 0 end) '2',
-> max(case month when '3' then score else 0 end) '3',
-> max(case month when '4' then score else 0 end) '4'
-> from score
-> group by name,year
-> order by year desc;
+------+------+------+------+------+------+
| name | year | 1 | 2 | 3 | 4 |
+------+------+------+------+------+------+
| 张三 | 2018 | 40 | 60 | 50 | 80 |
| 张三 | 2017 | 25 | 36 | 98 | 22 |
+------+------+------+------+------+------+
qq_32711805 2018-08-13
  • 打赏
  • 举报
回复
手机不好码代码,望采纳

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧