如何在SQL Query中使用sum()函数并选择列

时间:2015-03-31 05:58:22

标签: database oracle

我是数据库方面的新手。我需要总结两列,我还需要从特定列中获取数据。所以我写了这样一个查询:

select orderid,sum(productid),sum(price) from orderdetails

但这会返回错误说明

not a single-group group function

请指导我们。

3 个答案:

答案 0 :(得分:1)

您必须添加group by声明:

select orderid,sum(productid),sum(price) from orderdetails group by orderid

答案 1 :(得分:1)

  

如果你这样做,你不能简单地选择一个带有其他聚合函数调用的列   真的很想要,然后你需要使用聚合函数   列(orderid)也是,或者您需要放置该特定列   (orderidgroup by中的select max(orderid),sum(productid),sum(price) from orderdetails

像这样:

select orderid,sum(productid),sum(price) from orderdetails group by orderid

{{1}}

请参阅here其他聚合函数。

答案 2 :(得分:1)

select orderid,sum(productid),sum(price) from orderdetails group by orderid

无论何时使用aaggregate函数并且想要选择某些列,只需将所有列放在聚合函数之前,然后将列添加到该列