MySQL 将结果集转置为一行

备注:测试数据库版本为MySQL 8.0

如需要scott用户下建表及录入数据语句,可参考:
scott建表及录入数据sql脚本

一.需求

希望将几个行组中的数据转换成几行中的列,每个原来的行组转换成一行。
例如,下面的结果集显示了每个部门中员工的数目:

deptno cnt
10 3
20 5
30 6

希望重新设置输出格式,使其结果集看起来如下:
deptno_10 deptno_20 deptno_30
3 5 6

二.解决方案

2.1 解决方案1

使用sum汇总、case进行判断

select  sum(case when deptno = 10 then 1 else 0 end) as deptno_10,
        sum(case when deptno = 20 then 1 else 0 end) as deptno_20,
        sum(case when deptno = 30 then 1 else 0 end) as deptno_30
  from emp
 order by 1;

测试记录:

mysql> select  sum(case when deptno = 10 then 1 else 0 end) as deptno_10,
    ->         sum(case when deptno = 20 then 1 else 0 end) as deptno_20,
    ->         sum(case when deptno = 30 then 1 else 0 end) as deptno_30
    ->   from emp
    ->  order by 1;
+-----------+-----------+-----------+
| deptno_10 | deptno_20 | deptno_30 |
+-----------+-----------+-----------+
|         3 |         5 |         6 |
+-----------+-----------+-----------+
1 row in set (0.00 sec)

2.2 解决方案2

使用max及case函数

select max(case when deptno = 10 then empcount else null end) as deptno_10,
       max(case when deptno = 20 then empcount else null end) as deptno_20,
       max(case when deptno = 30 then empcount else null end) as deptno_30
  from (
select deptno, count(*) as empcount
  from emp
 group by deptno
       ) x;       

测试记录

mysql> select max(case when deptno = 10 then empcount else null end) as deptno_10,
    ->        max(case when deptno = 20 then empcount else null end) as deptno_20,
    ->        max(case when deptno = 30 then empcount else null end) as deptno_30
    ->   from (
    -> select deptno, count(*) as empcount
    ->   from emp
    ->  group by deptno
    ->        ) x;
+-----------+-----------+-----------+
| deptno_10 | deptno_20 | deptno_30 |
+-----------+-----------+-----------+
|         3 |         5 |         6 |
+-----------+-----------+-----------+
1 row in set (0.00 sec)

步骤进行分解

mysql> -- 根据部门进行分组
mysql> select deptno, count(*) as empcount
    ->   from emp
    ->  group by deptno;
+--------+----------+
| deptno | empcount |
+--------+----------+
|     10 |        3 |
|     20 |        5 |
|     30 |        6 |
+--------+----------+
3 rows in set (0.00 sec)

mysql>
mysql>
mysql> -- 使用case语句进行判断
mysql> select case when deptno = 10 then empcount else null end as deptno_10,
    ->        case when deptno = 20 then empcount else null end as deptno_20,
    ->        case when deptno = 30 then empcount else null end as deptno_30
    ->   from (
    -> select deptno, count(*) as empcount
    ->   from emp
    ->  group by deptno
    ->        ) x;
+-----------+-----------+-----------+
| deptno_10 | deptno_20 | deptno_30 |
+-----------+-----------+-----------+
|         3 |      NULL |      NULL |
|      NULL |         5 |      NULL |
|      NULL |      NULL |         6 |
+-----------+-----------+-----------+
3 rows in set (0.00 sec)

mysql>
mysql> -- 使用max函数求最大值
mysql> select max(case when deptno = 10 then empcount else null end) as deptno_10,
    ->        max(case when deptno = 20 then empcount else null end) as deptno_20,
    ->        max(case when deptno = 30 then empcount else null end) as deptno_30
    ->   from (
    -> select deptno, count(*) as empcount
    ->   from emp
    ->  group by deptno
    ->        ) x;
+-----------+-----------+-----------+
| deptno_10 | deptno_20 | deptno_30 |
+-----------+-----------+-----------+
|         3 |         5 |         6 |
+-----------+-----------+-----------+
1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值