我需要为SQL查询找到解决方案

问题描述 投票:0回答:1

我需要一些帮助来列出所有字段,并且仅列出11月从INGREDIENT表和INGREDIENT_PURCHASE_LIST表中进行的购买每个项目的总金额。按降序排序结果集。

CREATE TABLE ingredient
(
 ingredient_id NUMBER(4,0) PRIMARY KEY,
 ingredient_name VARCHAR2(100) NOT NULL
);



CREATE TABLE ingredient_purchase_list
(    
 ing_pl_id NUMBER(4,0) PRIMARY KEY, 
 date_ordered  DATE ,
 quantity VARCHAR2(15),
 unit VARCHAR(15),
 unit_price  NUMBER(4,2) NOT NULL,
 ingredient_id NUMBER(4,0),   
   CONSTRAINT ingredient_id_fk  FOREIGN KEY (ingredient_id)
   REFERENCES ingredient (ingredient_id));

我有这个:

 SELECT i.ingredient_id, i.ingredient_name, ip.date_ordered, ip.quantity, ip.unit, ip.unit_price, (SUM(ip.unit_price * ip.quantity)) "TOTAL" 
 FROM ingredient_purchase_list ip, ingredient i
 WHERE ip.date_ordered BETWEEN '11-01-2019' AND '11-30-2019';
 GROUP BY ip.date_ordered;

我收到此错误:

ORA-00937:不是单组分组功能

sql
1个回答
0
投票

我发现了2个问题。

  1. 您的两张桌子之间的链接断开了
  2. 在列表中包括那些未汇总的列
SELECT i.ingredient_id, i.ingredient_name, ip.date_ordered, ip.quantity, ip.unit
    , ip.unit_price, (SUM(ip.unit_price * ip.quantity)) "TOTAL" 
FROM ingredient_purchase_list ip
INNER JOIN ingredient i on i.ingredient_id = ip.ingredient_id
WHERE ip.date_ordered BETWEEN '11-01-2019' AND '11-30-2019'
GROUP BY ip.date_ordered, i.ingredient_id, i.ingredient_name, ip.quantity, ip.unit
    , ip.unit_price;
© www.soinside.com 2019 - 2024. All rights reserved.