-- DB2, MariaDB, MySQL SELECT order_num, order_date FROM Orders WHERE YEAR (order_date) = 2020 AND MONTH (order_date) = 1 ORDER BY order_date; -- Oracle, PostgreSQL SELECT order_num, order_date FROM Orders WHERE EXTRACT (year FROM order_date) = 2020 AND EXTRACT (month FROM order_date) = 1 ORDER BY order_date; -- PostgreSQL SELECT order_num, order_date FROM Orders WHERE DATE_PART('year', order_date) = 2020 AND DATE_PART('month', order_date) = 1 ORDER BY order_num; -- SQL Server SELECT order_num, order_date FROM Orders WHERE DATEPART (yy, order_date) = 2020 AND DATEPART (mm, order_date) = 1 ORDER BY order_date; -- SQLite SELECT order_num FROM Orders WHERE strftime('%Y', order_date) = '2020' AND strftime('%m', order_date) = '01';
第 9 课 汇总数据
1. 编写SQL语句,确定已售出产品的总数(使用OrderItems 中的quantity列)。
1 2
SELECT SUM (quantity) AS items_ordered FROM OrderItems;
2. 修改刚刚创建的语句,确定已售出产品项(prod_item)BR01的总数。
1 2 3
SELECT SUM (quantity) AS items_ordered FROM OrderItems WHERE prod_id = 'BR01';
SELECT order_num, SUM (item_price*quantity) AS total_price FROM OrderItems GROUP BY order_num HAVING SUM (item_price*quantity) >= 1000 ORDER BY order_num;
5. 下面的SQL语句有问题吗?(尝试在不运行的情况下指出。)
1 2 3 4 5
SELECT order_num, COUNT(*) AS items FROM OrderItems GROUP BY items HAVING COUNT(*) >= 3 ORDER BY items, order_num;
GROUP BY项是错误的。GROUP BY必须是实际列,而不是用于执行汇总计算的列。允许使用GROUP BY order_num。
SELECT cust_email FROM Customers WHERE cust_id IN ( SELECT cust_id FROM Orders WHERE order_num IN ( SELECT order_num FROM OrderItems WHERE prod_id = 'BR01'));
SELECT cust_id, ( SELECT SUM (item_price*quantity) FROM OrderItems WHERE Orders.order_num = OrderItems.order_num) AS total_ordered FROM Orders ORDER BY total_ordered DESC ;
SELECT cust_name, order_num FROM Customers, Orders WHERE Customers.cust_id = Orders.cust_id ORDER BY cust_name, order_num; -- ANSI INNER JOIN语法 SELECT cust_name, order_num FROM Customers INNER JOIN Orders ON Customers.cust_id = Orders.cust_id ORDER BY cust_name, order_num;
SELECT cust_name, order_num, ( SELECT Sum(item_price*quantity) FROM OrderItems WHERE Orders.order_num=OrderItems.order_num) AS OrderTotal FROM Customers, Orders WHERE Customers.cust_id = Orders.cust_id ORDER BY cust_name, order_num; -- 使用联结的解法 SELECT cust_name, Orders.order_num, Sum(item_price*quantity) AS OrderTotal FROM Customers, Orders, OrderItems WHERE Customers.cust_id = Orders.cust_id AND Orders.order_num = OrderItems.order_num GROUP BY cust_name, Orders.order_num ORDER BY cust_name, order_num;
SELECT cust_name, SUM (item_price*quantity) AS total_price FROM Customers, Orders, OrderItems WHERE Customers.cust_id = Orders.cust_id AND Orders.order_num = OrderItems.order_num GROUP BY cust_name HAVING SUM (item_price*quantity) >= 1000 ORDER BY cust_name;
– ANSI INNER JOIN 语法
1 2 3 4 5 6 7
SELECT cust_name, SUM (item_price*quantity) AS total_price FROM Customers INNER JOIN Orders ON Customers.cust_id = Orders.cust_id INNER JOIN OrderItems ON Orders.order_num = OrderItems.order_num GROUP BY cust_name HAVING SUM (item_price*quantity) >= 1000 ORDER BY cust_name;
SELECT prod_name, order_num FROM Products LEFT OUTER JOIN OrderItems ON Products.prod_id = OrderItems.prod_id ORDER BY prod_name;
4. 修改上一题中创建的SQL语句,使其返回每一项产品的总订单数(不是订单号)。
1 2 3 4 5
SELECT prod_name, COUNT (order_num) AS orders FROM Products LEFT OUTER JOIN OrderItems ON Products.prod_id = OrderItems.prod_id GROUP BY prod_name ORDER BY prod_name;
SELECT prod_id, quantity FROM OrderItems WHERE quantity = 100 UNION SELECT prod_id, quantity FROM OrderItems WHERE prod_id LIKE 'BNBG%' ORDER BY prod_id;
2. 重写刚刚创建的SQL语句,仅使用单个SELECT语句。
1 2 3
SELECT prod_id, quantity FROM OrderItems WHERE quantity = 100 OR prod_id LIKE 'BNBG%' ORDER BY prod_id;
SELECT prod_name FROM Products UNION SELECT cust_name FROM Customers ORDER BY prod_name;
4. 下面的SQL语句有问题吗?(尝试在不运行的情况下指出。)
1 2 3 4 5 6 7 8
SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state = 'MI' ORDER BY cust_name; UNION SELECT cust_name, cust_contact, cust_email FROM Customers WHERE cust_state = 'IL'ORDER BY cust_name;
-- MySQL, MariaDB, Oracle, PostgreSQL, SQLite CREATE TABLE OrdersBackup AS SELECT * FROM Orders; CREATE TABLE OrderItemsBackup AS SELECT * FROM OrderItems; -- SQL Server SELECT * INTO OrdersBackup FROM Orders; SELECT * INTO OrderItemsBackup FROM OrderItems;
CREATE VIEW CustomersWithOrders AS SELECT Customers.cust_id, Customers.cust_name, Customers.cust_address, Customers.cust_city, Customers.cust_state, Customers.cust_zip, Customers.cust_country, Customers.cust_contact, Customers.cust_email FROM Customers JOIN Orders ON Customers.cust_id = Orders.cust_id;
1
SELECT * FROM CustomersWithOrders;
2. 下面的SQL语句有问题吗?(尝试在不运行的情况下指出。)
1 2 3 4 5 6 7 8
CREATE VIEW OrderItemsExpanded AS SELECT order_num, prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM OrderItems ORDER BY order_num;