Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GROUP BY / aggregate function confusion in SQL

Tags:

sql

oracle

I need a bit of help straightening out something, I know it's a very easy easy question but it's something that is slightly confusing me in SQL.

This SQL query throws a 'not a GROUP BY expression' error in Oracle. I understand why, as I know that once I group by an attribute of a tuple, I can no longer access any other attribute.

SELECT *  FROM order_details  GROUP BY order_no 

However this one does work

SELECT SUM(order_price) FROM order_details GROUP BY order_no 

Just to concrete my understanding on this.... Assuming that there are multiple tuples in order_details for each order that is made, once I group the tuples according to order_no, I can still access the order_price attribute for each individual tuple in the group, but only using an aggregate function?

In other words, aggregate functions when used in the SELECT clause are able to drill down into the group to see the 'hidden' attributes, where simply using 'SELECT order_no' will throw an error?

like image 321
Chris Avatar asked Jan 06 '11 05:01

Chris


People also ask

Can I use aggregate function with GROUP BY?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How does using GROUP BY in a statement affect aggregates?

Aggregations Can Be Filtered Using The HAVING Clause The alternate having is placed after the group by and allows you to filter the returned data by an aggregated column. Using having, you can return the aggregate filtered results!

Do you have to use GROUP BY with aggregate functions SQL?

If you don't specify GROUP BY , aggregate functions operate over all the records selected. In that case, it doesn't make sense to also select a specific column like EmployeeID .

Why we Cannot use WHERE with GROUP BY?

Confusing WHERE and HAVING This statement will return an error because you cannot use aggregate functions in a WHERE clause. WHERE is used with GROUP BY when you want to filter rows before grouping them.


2 Answers

In standard SQL (but not MySQL), when you use GROUP BY, you must list all the result columns that are not aggregates in the GROUP BY clause. So, if order_details has 6 columns, then you must list all 6 columns (by name - you can't use * in the GROUP BY or ORDER BY clauses) in the GROUP BY clause.

You can also do:

SELECT order_no, SUM(order_price)   FROM order_details  GROUP BY order_no; 

That will work because all the non-aggregate columns are listed in the GROUP BY clause.

You could do something like:

SELECT order_no, order_price, MAX(order_item)   FROM order_details  GROUP BY order_no, order_price; 

This query isn't really meaningful (or most probably isn't meaningful), but it will 'work'. It will list each separate order number and order price combination, and will give the maximum order item (number) associated with that price. If all the items in an order have distinct prices, you'll end up with groups of one row each. OTOH, if there are several items in the order at the same price (say £0.99 each), then it will group those together and return the maximum order item number at that price. (I'm assuming the table has a primary key on (order_no, order_item) where the first item in the order has order_item = 1, the second item is 2, etc.)

like image 129
Jonathan Leffler Avatar answered Oct 03 '22 23:10

Jonathan Leffler


SELECT *  FROM order_details  GROUP BY order_no 

In the above query you are selecting all the columns because of that its throwing an error not group by something like.. to avoid that you have to mention all the columns whichever in select statement all columns must be in group by clause..

 SELECT *      FROM order_details      GROUP BY order_no,order_details,etc 

etc it means all the columns from order_details table.

like image 25
DoOrDie Avatar answered Oct 03 '22 22:10

DoOrDie