简体   繁体   中英

mysql pivot table by date

I'm trying to display results of a payment table containing the sum of specific columns by day;

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

What i'd like to display is the following;

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

is there a way of doing this?

Many thanks.

One option is to use conditional aggregation with sum and case :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM