2016-03-03 22 views
6

Tôi đã thử các ví dụ sau đây trong Oracle 11g: http://joshualande.com/filters-joins-aggregations/ORA-00.979: không phải là một GROUP BY biểu hiện với một ví dụ đơn giản

SELECT c.recipe_name, 
COUNT(a.ingredient_id), 
SUM(a.amount*b.ingredient_price) 
FROM recipe_ingredients a 
JOIN ingredients b 
ON a.ingredient_id = b.ingredient_id 
JOIN recipes c 
ON a.recipe_id = c.recipe_id 
GROUP BY a.recipe_id; 

tôi nhận được Lỗi SQL: ORA-00979: not a GROUP BY expression...

Các bảng được sử dụng trong truy vấn như sau:

CREATE TABLE recipes (
    recipe_id INT NOT NULL, 
    recipe_name VARCHAR(30) NOT NULL, 
    PRIMARY KEY (recipe_id), 
    UNIQUE (recipe_name) 
); 

INSERT INTO RECIPES (RECIPE_ID, RECIPE_NAME) VALUES (1, 'Tacos'); 
INSERT INTO RECIPES (recipe_id, recipe_name) VALUES (2, 'Tomato Soup'); 
INSERT INTO RECIPES (recipe_id, recipe_name) VALUES (3, 'Grilled Cheese'); 

CREATE TABLE ingredients (
    ingredient_id INT NOT NULL, 
    ingredient_name VARCHAR(30) NOT NULL, 
    ingredient_price INT NOT NULL, 
    PRIMARY KEY (ingredient_id), 
    UNIQUE (ingredient_name) 
); 

INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (1, 'Beef', 5); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (2, 'Lettuce', 1); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (3, 'Tomatoes', 2); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (4, 'Taco Shell', 2); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (5, 'Cheese', 3); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (6, 'Milk', 1); 
INSERT INTO ingredients (ingredient_id, ingredient_name, ingredient_price) VALUES (7, 'Bread', 2); 

CREATE TABLE recipe_ingredients (
    recipe_id int NOT NULL, 
    ingredient_id INT NOT NULL, 
    amount INT NOT NULL, 
    PRIMARY KEY (recipe_id,ingredient_id) 
); 

INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (1,1,1); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (1,2,2); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (1,3,2); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (1,4,3); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (1,5,1); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (2,3,2); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (2,6,1); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (3,5,1); 
INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES (3,7,2); 

Tôi biết câu hỏi này đã được hỏi nhiều lần rồi, nhưng plz giải thích cho tôi máng ví dụ này.

+0

Các GROUP chung THEO quy tắc nói: "Nếu một mệnh đề GROUP BY được chỉ định, mỗi tài liệu tham khảo cột trong danh sách SELECT phải hoặc là xác định một cột nhóm hoặc là đối số của hàm được đặt. " – jarlh

Trả lời

4

Trong mệnh đề SELECT, bạn chỉ có thể tham khảo biểu hiện mà một trong hai xuất hiện trong mệnh đề GROUP BY hoặc là quy tụ (như SUM). c.recipe_name không đủ điều kiện như vậy.

Bạn có thể biết rằng nhóm theo a.recipe_id sẽ dẫn đến kết quả duy nhất cho c.recipe_name (trong mỗi nhóm). Và Oracle thậm chí có thể lấy được thông tin này. Nhưng SQL nghiêm ngặt hơn và yêu cầu bạn đặt biểu thức trong mệnh đề GROUP BY.

Vì vậy, chỉ cần viết:

SELECT c.recipe_name, 
    COUNT(a.ingredient_id), 
    SUM(a.amount*b.ingredient_price) 
FROM recipe_ingredients a 
JOIN ingredients b 
    ON a.ingredient_id = b.ingredient_id 
JOIN recipes c 
    ON a.recipe_id = c.recipe_id 
GROUP BY a.recipe_id, c.recipe_name; 
+0

Cảm ơn bạn đã giải thích. Nó là tất cả rõ ràng bây giờ. – dsz36

3

Bạn còn phải đưa lĩnh vực c.recipe_name trong mệnh đề GROUP BY:

SELECT c.recipe_name, 
     COUNT(a.ingredient_id) AS cnt, 
     SUM(a.amount*b.ingredient_price) AS sum 
FROM recipe_ingredients a 
JOIN ingredients b 
    ON a.ingredient_id = b.ingredient_id 
JOIN recipes c 
    ON a.recipe_id = c.recipe_id 
GROUP BY c.recipe_name, a.recipe_id; 

Vấn đề với truy vấn của bạn là một cột không tổng hợp, như c.recipe_name, xuất hiện trong mệnh đề SELECT.

Output:

recipe_name  cnt sum 
------------------------ 
Grilled Cheese 2 7 
Tacos   5 20 
Tomato Soup  2 5 
+0

Thx để giải thích. – dsz36

0
SELECT 
    c.recipe_name, 
    COUNT(a.ingredient_id), 
    SUM(a.amount*b.ingredient_price) 
FROM recipe_ingredients a 
JOIN ingredients b 
    ON a.ingredient_id = b.ingredient_id 
JOIN recipes c 
    ON a.recipe_id = c.recipe_id 
GROUP BY c.recipe_name 
+0

Có lẽ bạn nên cố gắng giải thích lý do tại sao điều này sẽ hoạt động. – Cyclonecode

Các vấn đề liên quan