mysql数据透视表(按日期)


mysql pivot table by date

我试图显示一个付款表的结果,该表包含按天计算的特定列的总和;

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

有办法做到这一点吗?

非常感谢。

一个选项是将conditional aggregationsumcase:一起使用

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)