SELECT prod_name FROM products WHERE (unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products);
CREATE OR REPLACE VIEW test.products AS SELECT p.prod_id, p.prod_name, p.prod_status, 10 AS qty_in_hand, p.prod_list_price AS unit_price FROM sh.products p WHERE rownum <= 10;
要求取最高unit_price * qty_in_hand的name,得到的应该是一条数据
SQL> SELECT prod_name 2 FROM products 3 WHERE (unit_price * qty_in_hand) = 4 (SELECT MAX(unit_price * qty_in_hand) FROM products); PROD_NAME -------------------------------------------------- Envoy Ambassador 1 row selected
SQL> SELECT prod_name 2 FROM products 3 WHERE (unit_price * qty_in_hand) = 4 (SELECT MAX(unit_price * qty_in_hand) 5 FROM products 6 GROUP BY prod_name); SELECT prod_name FROM products WHERE (unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products GROUP BY prod_name) ORA-01427: single-row subquery returns more than one row
SQL> SELECT prod_name 2 FROM products 3 GROUP BY prod_name 4 HAVING MAX(unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) 5 FROM products 6 GROUP BY prod_name); SELECT prod_name FROM products GROUP BY prod_name HAVING MAX(unit_price * qty_in_hand) = (SELECT MAX(unit_price * qty_in_hand) FROM products GROUP BY prod_name) ORA-01427: single-row subquery returns more than one row
SQL> SELECT MAX(SUM(unit_price * qty_in_hand)) FROM products; SELECT MAX(SUM(unit_price * qty_in_hand)) FROM products ORA-00978: nested group function without GROUP BY