-2

I have a table with the following columns:

PRODUCT_ID | PRODUCT_NAME | GROUP_ID | GROUP_NAME | PRICE

GROUP_ID and GROUP_NAME are not UNIQUE. Can I make a selection which just returns each GROUP_ID with its GROUP_NAME and its SUM(PRICE)?

Or will I have to make a SELECT query for each GROUP?

For example: I have 12000 PRODUCTs which are in 14 GROUPs. My query should then only return 14 rows.

1
  • 2
    Although probably not relevant for such a basic question, you should always mention the DBMS you are using (Postgres, Oracle, DB2, ...) Mar 24 '15 at 18:36
0

a SUM is an aggregation of data, when working with aggregations you can specify the clause GROUP BY to show results grouped by what you need.

SELECT Group_Name, 
       SUM(Price)
  FROM Table
 GROUP BY Group_Name
-1
SELECT GROUP_ID, Group_Name, 
       SUM(Price)
  FROM ProductsTable
 GROUP BY GROUP_ID, Group_Name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.