简体   繁体   English

SQL添加具有相同ID的不同行的值

[英]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 BYSUM一起使用。 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.

相关问题 SQL SELECT ID WHERE 具有相同 ID 的行具有不同的值 - SQL SELECT ID WHERE rows with the same ID have different Values SQL合并具有相同ID但不同列值的两行(Oracle) - SQL Merge two rows with same ID but different column values (Oracle) 在Oracle SQL中合并具有相同ID但不同列值的行 - Merge Rows with Same ID but Different Column Values in Oracle SQL SQL查询从具有相同ID的不同行中选择值 - SQL query that selects values from different rows that has same id 选择具有相同id但不同值的不同行 - Selecting different rows with same id but different values 在多行中填充的SQL相同ID具有不同的列值-在一行中需要一个ID - SQL Same ID Populated in Multiple Rows with Different Column Values Populated--Need One ID in One Row 在SQL中查找不同列具有相同值的行 - Find the rows with same values of different columns in SQL 当需要比较同一表但具有唯一ID值的不同行时,可以使用Oralce SQL嵌套或内部联接 - Oralce SQL nested or inner join when you need to compare the same table but different rows with unique ID values 在Apache Spark Sql Context中,如何删除具有相同ID但不同行值的行 - In Apache Spark Sql Context how to delete rows with same ID but different row values SQL在一列中获取具有相同ID但值不同的所有行 - SQL get all rows which have the same id but different values in one column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM