[英]SQL add values of different rows with same ID
Query:询问:
SELECT code, duration
FROM table1
results in结果是
code代码 | duration期间 |
---|---|
234 234 | 3 3 |
345 345 | 2 2 |
345 345 | 3 3 |
456 456 | 4 4 |
567 567 | 5 5 |
567 567 | 6 6 |
567 567 | 1 1 |
I would like to add the durations of the same code and display results as here我想在这里添加相同代码的持续时间并显示结果
code代码 | duration期间 |
---|---|
234 234 | 3 3 |
345 345 | 5 5 |
456 456 | 4 4 |
567 567 | 12 12 |
but I don't know where to start with this query?但我不知道从哪里开始这个查询?
你使用聚合函数SUM
SELECT code, SUM(duration) FROM table1 GROUP BY code
You can use GROUP BY
together with SUM
.您可以将GROUP BY
与SUM
一起使用。 When using GROUP BY
, aggregate functions (such as SUM
) will return the output of the function as if every value in the specified column for the entire group was passed to it.使用GROUP BY
时,聚合函数(例如SUM
)将返回函数的输出,就好像整个组的指定列中的每个值都传递给它一样。 In this case you can tell it to run on duration
.在这种情况下,您可以告诉它在duration
上运行。
SELECT code, SUM(duration) as TotalDuration FROM table1 GROUP BY code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.