简体   繁体   中英

Oracle SQL GROUP BY issues in sub query

I'm having issues getting my command to work and it seems that no matter what I do I continue to get an Oracle error on the SELECT customer_no statement.

It is always either a Not a Group By error, or a Not a single-group group function error.

The goal is to create a query that finds all the customers who have ordered styrofoam and with a defined size. For those customers, I also want to find their first and last styrofoam order.

SELECT DISTINCT customer_no, MIN(date), MAX(date)
FROM    (SELECT customer_no, details, city, size, order_item, state, date
         FROM (((customer JOIN region USING (location)) JOIN order USING (order_no)) 
         JOIN rep USING (emp_no))
         WHERE order_item = 'Styrofoam' AND size IS NOT NULL
         GROUP BY details, size, date, city, order_item, state);

You have no customer_no in the group by . This is causing the error. But, why all the complication:

SELECT customer_no, MIN(date), MAX(date)
FROM  customer JOIN
      region
      USING (location) JOIN
      order
      USING (order_no) JOIN
      rep USING (emp_no)
WHERE order_item = 'Styrofoam' AND size IS NOT NULL
GROUP BY customer_no;

The names of the tables suggest that all the joins are not necessary. For instance, none of the columns obviously would come from a table called rep .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM