sql - 在 MS ACCESS 中使用 SELF JOIN 对多列求和
问题描述
我有一张桌子 - table1 如下所示:
+------+--------+--------+-----+
ID---hours1---hours2-----hours3--+
1-------4-------3---------2----+
2-------8-------7---------6----+
1-------5-------2---------1----+
2-------10------11--------2----+
预期成绩:
ID-----Total
1--------17
2--------44
我尝试了如下 SELF 连接查询:
SELECT ID, SUM(hours1 + hours2 + hours3)
from table1 a inner join table1 b ON a.Id = b.Id
group by a.Id
然而,这给出了不正确的结果,结果非常奇怪。谁能帮我上面的查询有什么问题?
解决方案
我认为不需要自我加入,只是:
select t.id, sum(t.hours1+t.hours2+t.hours3) as total
from table1 t
group by t.id
推荐阅读
- django - 将 Sql 查询(多对多)转换为 Django ORM 查询
- node.js - node.js 版本升级后如何重建节点模块?
- c++ - 我想定义一个类来接收来自用户的一些信息,然后在 C++ 控制台中打印它们
- r - 尝试创建一个新列,观察响应 2 个单独列中的标准并确定输出
- r - 使用循环从 R 中数据框中的另一列创建多列
- php - 如何在 php 中解码 utf-8
- r - 在 r-highcharter 中更改图表元素的颜色
- c# - EF Add-Migration:使用 app.config 的连接字符串问题
- python - 如何将类的实例分配为类主体中同一类的属性?
- wordpress - 如何在提交按钮下方放置“接受”?Elementor Pro Form + 内联输入问题