SQL查询以列出员工及其在数据库中进行的订单数量。输出列表必须包括员工ID,姓名和订单数量。该列表必须按降序排列的订单数量排序。我引用了下面的必填表。
CREATE TABLE employees
(
employee_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 108
PRIMARY KEY,
first_name VARCHAR( 255 ) NOT NULL,
last_name VARCHAR( 255 ) NOT NULL,
email VARCHAR( 255 ) NOT NULL,
phone VARCHAR( 50 ) NOT NULL ,
hire_date DATE NOT NULL ,
manager_id NUMBER( 12, 0 ) , -- fk
job_title VARCHAR( 255 ) NOT NULL,
CONSTRAINT fk_employees_manager
FOREIGN KEY( manager_id )
REFERENCES employees( employee_id )
ON DELETE CASCADE
);
-- product category
CREATE TABLE product_categories
(
category_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 6
PRIMARY KEY,
category_name VARCHAR2( 255 ) NOT NULL
);
-- products table
CREATE TABLE products
(
product_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 289
PRIMARY KEY,
product_name VARCHAR2( 255 ) NOT NULL,
description VARCHAR2( 2000 ) ,
standard_cost NUMBER( 9, 2 ) ,
list_price NUMBER( 9, 2 ) ,
category_id NUMBER NOT NULL ,
CONSTRAINT fk_products_categories
FOREIGN KEY( category_id )
REFERENCES product_categories( category_id )
ON DELETE CASCADE
);
-- customers
CREATE TABLE customers
(
customer_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 320
PRIMARY KEY,
name VARCHAR2( 255 ) NOT NULL,
address VARCHAR2( 255 ) ,
website VARCHAR2( 255 ) ,
credit_limit NUMBER( 8, 2 )
);
CREATE TABLE orders
(
order_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 106
PRIMARY KEY,
customer_id NUMBER( 6, 0 ) NOT NULL, -- fk
status VARCHAR( 20 ) NOT NULL ,
salesman_id NUMBER( 6, 0 ) , -- fk
order_date DATE NOT NULL ,
CONSTRAINT fk_orders_customers
FOREIGN KEY( customer_id )
REFERENCES customers( customer_id )
ON DELETE CASCADE,
CONSTRAINT fk_orders_employees
FOREIGN KEY( salesman_id )
REFERENCES employees( employee_id )
ON DELETE SET NULL
);
-- order items
CREATE TABLE order_items
(
order_id NUMBER( 12, 0 ) , -- fk
item_id NUMBER( 12, 0 ) ,
product_id NUMBER( 12, 0 ) NOT NULL , -- fk
quantity NUMBER( 8, 2 ) NOT NULL ,
unit_price NUMBER( 8, 2 ) NOT NULL ,
CONSTRAINT pk_order_items
PRIMARY KEY( order_id, item_id ),
CONSTRAINT fk_order_items_products
FOREIGN KEY( product_id )
REFERENCES products( product_id )
ON DELETE CASCADE,
CONSTRAINT fk_order_items_orders
FOREIGN KEY( order_id )
REFERENCES orders( order_id )
ON DELETE CASCADE
);
我尝试过的。
SELECT E.EMPLOYEE_ID,
E.FIRSTNAME
|| ' '
|| E.LASTNAME,
SUM(OI.QUANTITY * OI.UNITPRICE) AS TOTAL_SALES
FROM ORDERS O
JOIN EMPLOYEES E ON E.EMPLOYEE_ID = O.SALESMAN_ID
JOIN ORDER_ITEMS OI ON O.ORDER_ID = OI.ORDER_ID
GROUP BY E.EMPLOYEE_ID,
E.FIRSTNAME,
E.LASTNAME
ORDER BY TOTAL_SALES DESC NULLS LAST;
答案 0 :(得分:0)
您可以按以下方式使用GROUP BY
,AGGREGATION
和ORDER BY
函数:
SELECT E.EMPLOYEE_ID,
E.FIRSTNAME
|| ' '
|| E.LASTNAME,
SUM(OI.QUANTITY * OI.UNITPRICE) AS TOTAL_SALES
FROM ORDERS O
JOIN EMPLOYEES E ON E.EMPLOYEE_ID = O.SALESMAN_ID
JOIN ORDER_ITEMS OI ON O.ORDER_ID = OI.ORDER_ID
GROUP BY E.EMPLOYEE_ID,
E.FIRSTNAME,
E.LASTNAME
ORDER BY 3 DESC NULLS LAST;
答案 1 :(得分:0)
你能这样尝试吗?
SELECT EMPLOYEE_ID,
EMPNAME,
TOTAL_SALES
FROM (
SELECT E.EMPLOYEE_ID,
E.FIRSTNAME
|| ' '
|| E.LASTNAME EMPNAME,
SUM(OI.QUANTITY * OI.UNITPRICE) AS TOTAL_SALES
FROM ORDERS O
JOIN EMPLOYEES E ON E.EMPLOYEE_ID = O.SALESMAN_ID
JOIN ORDER_ITEMS OI ON O.ORDER_ID = OI.ORDER_ID
GROUP BY E.EMPLOYEE_ID,
E.FIRSTNAME,
E.LASTNAME)
ORDER BY TOTAL_SALES DESC NULLS LAST;