[英]MySql: select total grouped by two columns
I have a table transactions
like this: http://prntscr.com/aa5vg6 I want to select total amount group by purpose
and date
column. 我有一个这样的表
transactions
: http : //prntscr.com/aa5vg6我想按purpose
和date
列选择总额组。 Such as: found 10 records for purpose 1, 21 records for purpose 2 and so on... in the date 2016-02-01, 2016-02-05 and so on... the result should be total sum of purpose and the purpose total should be grouped by date. 例如:在日期2016-02-01、2016-02-05中找到了目的1的10条记录,目的2的21条记录,依此类推...在日期2016-02-01、2016-02-05等中,结果应为目标和目的总计应按日期分组。
I tried this query, but it fails grouping the date while multiple purpose found in same date: 我尝试了此查询,但未能在同一日期中找到多个目的的同时对日期进行分组:
SELECT SUM(amount) totalAmount FROM `transactions` WHERE purpose IN(1,2,12) GROUP BY date ORDER BY date DESC
You should group by date and purpose in that case: 在这种情况下,您应该按日期和目的分组:
SELECT
DATE(`date`) date,
`purpose`,
COUNT(`purpose`) purposeCount,
SUM(`amount`) totalAmount
FROM
`transactions`
WHERE
`purpose` IN (1, 2, 12)
GROUP BY
DATE(`date`),
`purpose`
ORDER BY
DATE(`date`) DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.