我有一个Table SalesInvoice。在此日期中,金额模式是此表中的重要列。
SNo BID iDate Amount Mode
----------------------------------------------------------------
101 1 2016-01-01 200 Cash
102 2 2016-01-01 500 Credit
103 5 2016-01-01 800 credit
104 8 2016-01-01 250 Cash
105 1 2016-01-01 200 Cash
106 2 2016-01-02 500 Cash
107 5 2016-01-03 800 Credit
108 8 2016-01-03 250 Credit
我的要求输出是
iDate MinSno MaxSno Total CreditTotal CashTotal
----------------------------------------------------------------------
2016-01-01 101 105 1950 1300 650
2016-01-02 106 106 500 0 500
2016-01-03 107 108 1050 1050 0
我尝试了MySQL查询
SELECT iDate, Min(SNo) AS MinSno, Max(SNo) AS MaxSno, SUM(Amount)
From SalesInvoice GROUP By iDate
但是,我不知道如何根据主要集团iDate中的子集团获得现金和信贷金额。请帮助我,如何达到上述预期的产量?
答案 0 :(得分:1)
这似乎有效:
SELECT iDate, min(SNo) as MinSno, max(SNo) as MaxSno, sum(amount) as Total,
sum(If(MMode = 'Cash',Amount,0)) as Cash,
sum(If(MMode = 'Credit',Amount,0)) as Credit
from test.SalesInvoice
Group By iDate;
请注意,我将Mode更改为MMode,因为Mode是MySQL中的关键工具