mysql - 使用 mysql 查询将数据显示为列标题
问题描述
我的数据看起来像
EmployeeId paycategory value
1 Contribution 200
1 Salary 18000
我想将结果显示为,
EmployeeId Salary Contribution
1 18000 200
我的尝试看起来像,
select EmployeeId,paycategory,value from Reports pi where employeeId = 1 and paycategory = 'Salary'
union
select EmployeeId,paycategory,value from Reports pi where employeeId = 1 and paycategory = 'Contribution'
结果显示为,
EmployeeId paycategory value
1 Contribution 200
1 Salary 18000
与原始数据格式相同
解决方案
使用条件聚合来透视您的数据集:
select employeeid,
max(case when paycategory = 'Salary' then value end) salary,
max(case when paycategory = 'Contribution' then value end) contribution
from reports
group by employeeid
推荐阅读
- html - ExtentReports:不在 log.info 消息中打印肥皂请求
- r - 如何在绘图饼图中硬编码显示变量的颜色?
- .net - 将 async 和 await 与 lambda 表达式一起使用?
- angular - 如何显示 amexio-d3-chart-donut?
- django - 我想根据用户信息动态改变django的形式
- python-3.x - 在 Python 二进制文件中包含 WinPcap
- c++ - 有没有更好的方法来添加两个智能指针?
- jquery - 数据表中未排序的结果
- r - 使用 R 从字符中删除停用词
- firebase - 谷歌云端硬盘手表变化的无服务器解决方案