not a single-group group function sum

2521 views sql
-1

So this is what I have

select idproduct, quantity from bb_basketitem order by idproduct; IDPRODUCT QUANTITY


2 1
4 1
4 1
6 1
6 1
6 2
7 1
7 1
7 1
7 1
7 1
7 1
7 1
8 1
8 1
8 1
8 1
8 1
8 2
8 1
8 1
8 2
9 2
9 2
9 1
9 1
10 1
10 2

28 rows selected

I want to display the total quantity for each product by product id, I feel like it should work like this

select idproduct, sum(quantity) from bb_basketitem order by idproduct;

But when I execute the query I get this error

Error starting at line 1 in command: select idproduct, sum(quantity) from bb_basketitem order by idproduct Error at Command Line:1 Column:7 Error report: SQL Error: ORA-00937: not a single-group group function 00937. 00000 - "not a single-group group function" *Cause:
*Action:

answered question

2 Answers

1

You are missing the group by clause. The query should be like:

select idproduct, sum(quantity) 
from bb_basketitem 
group by idproduct
order by idproduct;

posted this
10

Whenever you include an aggregate function in the select list of your SQL statement, you need to list all non-aggregated columns with a group by clause like below :

select col1, col2, sum(col0)
  from tab
 group by col1, col2;

posted this

Have an answer?

JD

Please login first before posting an answer.