gpt4 book ai didi

mysql - 如何将这些行连接到同一个表中的一条记录中

转载 作者:行者123 更新时间:2023-11-30 23:07:50 25 4
gpt4 key购买 nike

X表的原始数据

     Date       #  Method    #   Count     #     
======================================
2012-08-24 # A # 44 #
2012-08-24 # B # 5 #
2012-08-24 # D # 1 #

*p/s 一共有8种方法a,b,c,d,e,f,g,h

下面是我要显示的内容:

Date        # A  # B # C # D # E # F # G # H #
2012-08-24 # 44 # 5 # 0 # 1 # 0 # 0 # 0 # 0 #

最佳答案

试试这个:

SELECT A.Date, 
SUM(CASE WHEN A.Method = 'A' THEN A.Count ELSE 0 END) AS 'A',
SUM(CASE WHEN A.Method = 'B' THEN A.Count ELSE 0 END) AS 'B',
SUM(CASE WHEN A.Method = 'C' THEN A.Count ELSE 0 END) AS 'C',
SUM(CASE WHEN A.Method = 'D' THEN A.Count ELSE 0 END) AS 'D',
SUM(CASE WHEN A.Method = 'E' THEN A.Count ELSE 0 END) AS 'E',
SUM(CASE WHEN A.Method = 'F' THEN A.Count ELSE 0 END) AS 'F',
SUM(CASE WHEN A.Method = 'G' THEN A.Count ELSE 0 END) AS 'G',
SUM(CASE WHEN A.Method = 'H' THEN A.Count ELSE 0 END) AS 'H'
FROM tableA A
GROUP BY A.Date

关于mysql - 如何将这些行连接到同一个表中的一条记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087429/

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