gpt4 book ai didi

mysql - 将记录值显示为列

转载 作者:行者123 更新时间:2023-11-29 04:21:19 24 4
gpt4 key购买 nike

我有一个这样的表:

    X    |      Y      |  Z
---------+-------------+-----
3136 | 2014-03 | A
700 | 2014-03 | B
531 | 2014-03 | C
1575 | 2014-03 | D
2776 | 2014-04 | A
1200 | 2014-04 | B
148 | 2014-04 | C
903 | 2014-04 | D

我想把这张表转换成类似的东西:

Date        |  A   |  B   |  C   |  D
------------+------+------+------+------
2014-03 | 3136 | 700 | 531 | 1575
2014-04 | 2776 | 1200 | 148 | 903

如何通过一次选择完成?

最佳答案

SELECT y as Date,
SUM(CASE WHEN Z='A' THEN X ELSE 0 END) as A,
SUM(CASE WHEN Z='B' THEN X ELSE 0 END) as B,
SUM(CASE WHEN Z='C' THEN X ELSE 0 END) as C,
SUM(CASE WHEN Z='D' THEN X ELSE 0 END) as D
FROM table
GROUP BY y

关于mysql - 将记录值显示为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23136625/

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