2015-11-25 54 views
0

我试图显示包含特定列总和的付款表结果;按日期排列的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

http://stackoverflow.com/questions/14805851/mysql-sum-column-values-based-on-row-from-the -same-table – Standej

+1

请向我们展示您的尝试和获得的结果 – rkyr

+0

您可能想阅读[我如何提出一个好问题](http://stackoverflow.com/help/how-to-ask),它可以增强获得有用答案的概率_drastically_。你可能会发现[ESR](https://en.m.wikipedia.org/wiki/Eric_S._Raymond)的优秀论文[如何提问智能方式](http://catb.org/~esr/) faqs/smart-questions.html)也很有帮助。 –

回答

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)