gpt4 book ai didi

mysql - 如何按 'less than equal to' 分组对查询结果进行分组?

转载 作者:行者123 更新时间:2023-11-30 23:26:26 24 4
gpt4 key购买 nike

我需要计算具有特定状态 <= 特定日期的所有项目的数量。因此,如果我按“天”分组,则必须计算直到那一天的所有“开放”项目。因此,对于下一个“天”,它将是一个累计总和,包括前一天的“未结”项目。

这是架构的示例:

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

我对如何表述这个问题感到很困惑。我确定您需要一个 self (内部)加入,但我不知道如何进行分组以及究竟要计算什么,因为它是一个累计总和。我猜我需要一些临时变量等,但不确定如何在 MySQL 中执行此操作。

最佳答案

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

关于mysql - 如何按 'less than equal to' 分组对查询结果进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13079846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com