gpt4 book ai didi

php - mysql 按日期的数据透视表

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

我正在尝试显示包含按天特定列总和的付款表的结果;

payment id | payment_actual_timestamp | payment type | amount
1 | 2015-11-23 13:01:20 | AUTH | 0.16
2 | 2015-11-23 13:07:20 | AUTH | 23.65
3 | 2015-11-24 08:07:20 | VOID | 19.24
4 | 2015-11-24 13:45:20 | AUTH | 0.65
5 | 2015-11-24 16:34:10 | REFUND | 1.20
6 | 2015-11-25 13:07:20 | SETTLE | 4.40

我想显示的内容如下;

Date       | SETTLE | AUTH | VOID | REFUND
2015-11-23 | 0.00 | 23.81| 0.00 | 0.00
2015-11-24 | 0.00 | 0.65 | 19.24| 1.20
2015-11-25 | 4.40 | 0.00 | 0.00 | 0.00

有办法做到这一点吗?

非常感谢。

最佳答案

一种选择是将条件聚合sumcase结合使用:

select date(payment_actual_timestamp), 
sum(case when payment_type = 'SETTLE' then amount end) settleamt,
sum(case when payment_type = 'AUTH' then amount end) authamt,
sum(case when payment_type = 'VOID' then amount end) voidamt,
sum(case when payment_type = 'REFUND' then amount end) refundamt
from yourtable
group by date(payment_actual_timestamp)

关于php - mysql 按日期的数据透视表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33927985/

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