首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GROUP BY with OVER

GROUP BY with OVER
EN

Stack Overflow用户
提问于 2021-08-13 08:12:27
回答 3查看 59关注 0票数 0

我有两个表:

事务处理表

代码语言:javascript
复制
+----+
| id |
+----+
| 1  |
+----+
| 2  |
+----+
| 3  |
+----+

条目表

代码语言:javascript
复制
+----+----------------+--------+---------+
| id | transaction_id | income | expense |
+----+----------------+--------+---------+
| A  | 1              |        | 500     |
+----+----------------+--------+---------+
| B  | 2              | 250    |         |
+----+----------------+--------+---------+
| C  | 2              | 250    |         |
+----+----------------+--------+---------+

这个查询可以很好地工作:

代码语言:javascript
复制
SELECT e.income, e.expense, sum(e.income - e.expense) OVER (ORDER BY e.id) AS cumulative_sum
FROM entries e
JOIN transactions t on t.id = e.transaction_id;

但是我需要一个类似这样的查询(它不起作用):

代码语言:javascript
复制
SELECT sum(e.income), sum(e.expense), sum(e.income-e.expense) OVER (ORDER BY e.id) AS cumulative_sum
FROM entries e
JOIN transactions t on t.id = e.transaction_id
GROUP by t.id;

因此,我可以得到以下结果:

代码语言:javascript
复制
+----------------+--------+---------+---------+
| transaction_id | income | expense | balance |
+----------------+--------+---------+---------+
| 1              |        | 500     | 500     |
+----------------+--------+---------+---------+
| 2              | 500    |         | 0       |
+----------------+--------+---------+---------+

我从前面的查询中得到的错误:

代码语言:javascript
复制
Error : Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'e.income' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

该错误引用此处指定的列"sum(e.income - e.expense) OVER...“

那么,在这种情况下,我该如何实际使用OVER和GROUP BY呢?或者有没有其他的解决方案,使用带有交叉连接的派生表?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-08-13 08:30:48

我很惊讶sum(e.income - e.expense)为你工作。如果您的行中的收入或支出为NULL,则所有计算的差值都应为null,因此它们的总和也应为null。

您应该使用sum(e.income) - sum(e.expense),甚至在没有收入或根本没有支出的情况下使用COALESCE

无论如何,您希望从每个事务的简单聚合中获得:

代码语言:javascript
复制
SELECT
  t.id,
  SUM(e.income) AS total_income,
  SUM(e.expense) AS total_expense,
  COALESCE(SUM(e.income), 0) - COALESCE(SUM(e.expense), 0) AS balance
FROM entries e
JOIN transactions t on t.id = e.transaction_id
GROUP BY t.id
ORDER BY t.id;

到一个流动的余额总额。为此,只需添加

代码语言:javascript
复制
SUM(COALESCE(SUM(e.income), 0) - COALESCE(SUM(e.expense), 0))
  OVER (ORDER BY t.id) AS cumulated_balance
票数 2
EN

Stack Overflow用户

发布于 2021-08-13 08:18:38

如下所示:

代码语言:javascript
复制
SELECT sum(e.income)  OVER (ORDER BY e.id) AS cum_income
     , sum(e.expense) OVER (ORDER BY e.id) AS cum_expense
     , COALESCE(sum(e.income)  OVER (ORDER BY e.id), 0)
     - COALESCE(sum(e.expense) OVER (ORDER BY e.id), 0) AS cumulative_sum
  FROM entries e
  JOIN transactions t on t.id = e.transaction_id
 ORDER BY e.transaction_id, e.id
;

不确定您想要哪种排序。

票数 1
EN

Stack Overflow用户

发布于 2021-08-13 08:26:32

每个sum()都需要自己的OVER子句,而您不需要GROUP BY

代码语言:javascript
复制
SELECT e.transaction_id,
       sum(e.income) OVER (ORDER BY e.id) AS cumulative_income,
       sum(e.expense) OVER (ORDER BY e.id) AS cumulative_expense,
       sum(e.income) OVER (ORDER BY e.id)
       - sum(e.expense) OVER (ORDER BY e.id) AS cumulative_sum
       FROM entries e
            INNER JOIN transactions t
                       ON t.id = e.transaction_id;

或者,如果您不想重复OVER子句(但仍然需要将其分别分配给每个sum() ),则可以使用命名窗口。

代码语言:javascript
复制
SELECT e.transaction_id,
       sum(e.income) OVER w AS cumulative_income,
       sum(e.expense) OVER w AS cumulative_expense,
       sum(e.income) OVER w
       - sum(e.expense) OVER w AS cumulative_sum
       FROM entries e
            INNER JOIN transactions t
                       ON t.id = e.transaction_id
       WINDOW w AS (ORDER BY e.id);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68768899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档