I cant seem to see why this query is failing, in my group by clause I have the columns that are in my select yet im still getting the error. Why is this? Below is my query;
SELECT c.customer_first_name, c.customer_last_name, MAX(SUM(cost_line))
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
ORDER BY customer_numb;
getting this error
SQL Error: ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
You need to get rid of the l.cost_line in the GROUP BY, as mti2935 suggests, and also get rid of the max() function -- you can't use multiple aggregate functions like this.
SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
ORDER BY customer_numb;
I think the problem is that you should not have l.cost_line in the GROUP BY clause, because you are using this field in an aggregate function in the SELECT clause. Try it without l.cost_line in the GROUP BY clause, and see if that solves the problem.
If you want the max value, you have to wrap it all up in subquery, take a look:
SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
HAVING SUM(cost_line) = (
SELECT MAX(sum_cost_line)
FROM
(SELECT SUM(cost_line) sum_cost_line
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name) a
)
ORDER BY customer_numb;
Edit if you are using Oracle, you can make it simpler:
SELECT c.customer_first_name, c.customer_last_name, SUM(cost_line)
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
HAVING SUM(cost_line) = (
SELECT MAX(SUM(cost_line))
FROM customers c LEFT OUTER JOIN orders o USING(customer_numb)
LEFT OUTER JOIN order_lines l USING (order_numb)
GROUP BY c.customer_first_name, c.customer_last_name
)
ORDER BY customer_numb;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.