gpt4 book ai didi

mysql - 对于 SQL SERVER 中的 Group BY

转载 作者:行者123 更新时间:2023-11-29 07:35:51 25 4
gpt4 key购买 nike

请帮我选择查询

我的表喜欢

| ID | Name | Salary | Month    |
|----|------|--------|----------|
| 1 | A | 15000 | January |
| 2 | B | 20000 | January |
| 3 | C | 25000 | January |
| 4 | A | 14000 | February |
| 5 | B | 19000 | February |
| 6 | c | 24000 | February |
| 7 | A | 16000 | March |
| 8 | B | 21000 | March |
| 9 | C | 26000 | March |

我想要这样的结果

| Name | January | February | March |
|------|---------|----------|-------|
| A | 15000 | 14000 | 16000 |
| B | 20000 | 19000 | 21000 |
| C | 25000 | 24000 | 26000 |

最佳答案

create temporary table `tab`
(ID int, Name varchar(10), Salary int, Month varchar(30));


insert into `tab` (ID ,Name, Salary ,Month) -- insert all statement
values(9, 'C' ,26000 ,'March');

select *from tab;

select
name,
sum(case when month = 'January' then salary else null end) as January
,sum(case when month = 'February' then salary else null end) as February
,sum(case when month = 'March' then salary else null end) as March
from tab
group by name;

输出

name, January, February, March
A, 15000, 14000, 16000
B, 20000, 19000, 21000
C, 25000, 24000, 26000

关于mysql - 对于 SQL SERVER 中的 Group BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48898308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com