按日期的mysql数据透视表

时间:2015-11-25 23:06:24

标签: php mysql

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

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

有没有办法做到这一点?

非常感谢。

1 个答案:

答案 0 :(得分:1)

一种选择是将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)