gpt4 book ai didi

mySQL查询列中的总金额(月)

转载 作者:可可西里 更新时间:2023-11-01 07:49:13 26 4
gpt4 key购买 nike

我有以下格式的数据:

date (mm/dd/yyyy), desc (详细说明), category (a,b,c), tran_type(借方、贷方),花费金额(金额)。

我想获取以下格式的数据:

category  ||  tran_type   ||   Jan_total  ||  feb_total  ||  mar_total  
A || debit || $101 || $201 || $302
A || credit || $500 || $600 || $200

最佳答案

这个查询应该给你所需的结果。

SELECT category
,tran_type
,SUM(IF(month(date) = 1,ABS(amount),0)) as jan_total
,SUM(IF(month(date) = 2,ABS(amount),0)) as feb_total
,SUM(IF(month(date) = 3,ABS(amount),0)) as mar_total
,SUM(IF(month(date) = 4,ABS(amount),0)) as apr_total
,SUM(IF(month(date) = 5,ABS(amount),0)) as may_total
,SUM(IF(month(date) = 6,ABS(amount),0)) as jun_total
,SUM(IF(month(date) = 7,ABS(amount),0)) as jul_total
,SUM(IF(month(date) = 8,ABS(amount),0)) as aug_total
,SUM(IF(month(date) = 9,ABS(amount),0)) as sep_total
,SUM(IF(month(date) = 10,ABS(amount),0)) as okt_total
,SUM(IF(month(date) = 11,ABS(amount),0)) as nov_total
,SUM(IF(month(date) = 12,ABS(amount),0)) as dec_total
FROM transactions
WHERE YEAR(date) = '2011'
GROUP BY category, tran_type

如果您不想遇到麻烦,请不要忘记过滤年份。

关于mySQL查询列中的总金额(月),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926835/

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