如何为 sum() 输出多条记录
How do I output multiple records for sum()
我有以下 table:
表 1
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
| 3 | 19 | 5 |
+----+-----------+-------+
我正在尝试创建一个查询,该查询从经度 = 18 的 Table1 中选择所有数据,但仅输出分数总和大于 15 的记录。所以基本上,我正在寻找这个输出:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
+----+-----------+-------+
我一直在尝试多个查询,但其中 none 个查询给出了我正在寻找的内容,或者它们有一些语法错误。我试过这个:
select * from Table1 where longitude = 18 having
sum(score)>15
但它产生了这个结果:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
+----+-----------+-------+
如何设置查询格式以便输出多条记录而不是一条记录?我试过使用连接,但遇到了问题。
您缺少 GROUP BY
:
select longitude, sum(score) score_sum
from Table1
where longitude = 18
group by longitude
having sum(score)>15
你可以看看here。
您可以使用加窗函数求和,如下所示:
select * from (
select id, longitude, score, Sm = sum(score) over(partition by longitude) from #yourTable ) a
where a.sm > 15 and a.longitude = 18
我有以下 table:
表 1
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
| 3 | 19 | 5 |
+----+-----------+-------+
我正在尝试创建一个查询,该查询从经度 = 18 的 Table1 中选择所有数据,但仅输出分数总和大于 15 的记录。所以基本上,我正在寻找这个输出:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
| 2 | 18 | 10 |
+----+-----------+-------+
我一直在尝试多个查询,但其中 none 个查询给出了我正在寻找的内容,或者它们有一些语法错误。我试过这个:
select * from Table1 where longitude = 18 having sum(score)>15
但它产生了这个结果:
+----+-----------+-------+
| id | longitude | score |
+----+-----------+-------+
| 1 | 18 | 10 |
+----+-----------+-------+
如何设置查询格式以便输出多条记录而不是一条记录?我试过使用连接,但遇到了问题。
您缺少 GROUP BY
:
select longitude, sum(score) score_sum
from Table1
where longitude = 18
group by longitude
having sum(score)>15
你可以看看here。
您可以使用加窗函数求和,如下所示:
select * from (
select id, longitude, score, Sm = sum(score) over(partition by longitude) from #yourTable ) a
where a.sm > 15 and a.longitude = 18