如何通过'小于等于'分组来对查询结果进行分组?

时间:2012-10-26 02:06:25

标签: mysql

我需要计算具有特定状态< =特定日期的所有项目。因此,如果我按“日期”进行分组,那么所有“开放”项目必须计算在那一天。因此,对于下一个“日”,它将是累计金额,包括前一天的“未结”项目。

以下是架构的示例:

enter image description here

查询:特定日期有多少项目处于特定状态?

预期输出:

    day*| status | count
    ---------------------
     1  |  open  |  3
     2  |  open  |  6 //3 today and 3 from the previous day
     2  |  maybe |  1
     2  |  agree |  2
     2  |  open  |  6 //nothing new was 'open'
     3  |  open  |  8 //2 new
     3  |  agree |  4 //2 new

*could also be date or some other format. Not a rule, but just for illustration

我对如何制定这个问题感到非常难过。我确定你需要一个自我(内部)联接,但我不知道该怎么做分组,究竟要计算什么,因为它是累积总和。我猜我需要一些临时变量等但不知道如何在MySQL中这样做。

1 个答案:

答案 0 :(得分:0)

您可以尝试在GROUP BY之后使用WITH ROLLUP。 http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

相关问题