簡體   English   中英

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

有沒有辦法做到這一點?

非常感謝。

一種選擇是對sumcase使用conditional aggregation

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM