Professional Documents
Culture Documents
Information System Applications
Information System Applications
PERSONAL PROJECT
Customer_Name Product_Line
Customer_Type Product_Line_Name
Customer_Zipcode PL_Prior_Years_Sales_Goal
Customer_Years PL_Current_Year_Sales_Goal
Order_Number_of_Shipments Product_Price
Customer_ID Product_Cost
PR_Prior_Year_Sales_Goal
PR_Current_Year_Sales_Goal
Product_Line_Name
Invoice Payment
Invoice_Number Invoice_Number
Order_Number Payment_Date
INFORMATION
Invoice_Date SYSTEM APPLICATIONS
Payment_Amount – Personal Project 1
History_Invoice
Invoice_Number
Payment_Total
-- Query SQL 3: How many orders have we received FROM "Mai Lê Duẩn"
SELECT t.Customer_ID, COUNT(*) AS [Sum_Orders]
FROM T_Order t, Customer c
WHERE (t.Customer_ID = c.Customer_ID) and (Customer_Name = N'Mai Lê Duẩn')
GROUP BY t.Customer_ID;
*Hình ảnh minh họa:
-- Query SQL 4: What products have had total sales exceeding VND800000 during
the past month(June/2020)?
SELECT p.Product_ID, p.Product_Description,p.Product_Price*ol.Order_Quantity
as [Total]
FROM Order_Line ol, Product p, T_Order o
WHERE (ol.Product_ID = p.Product_ID) and (p.Product_Price*ol.Order_Quantity >
800000)
and (MONTH(o.Order_Placement_Date) = 6)
and (o.Order_Number = ol.Order_Number)
*Hình ảnh minh họa:
-- Query SQL 6: How many different items were ordered on order number 2?
SELECT COUNT(Product_ID) AS [Total]
FROM Order_Line
WHERE Order_Number = 2
*Hình ảnh minh họa:
-- Query SQL 7: How many different items were ordered on order number 4, and
what are they?
SELECT Order_Number, p.Product_ID,p.Product_Description, Order_Quantity,
INFORMATION SYSTEM APPLICATIONS – Personal Project
COUNT(ol.Product_ID) as[Total]
8
FROM Order_Line ol, Product p
WHERE (Order_Number = 4) and (p.Product_ID = ol.Product_ID)
GROUP BY Order_Number, p.Product_ID, p.Product_Description, Order_Quantity
*Hình ảnh minh họa:
-- Query SQL 10: List product_description, finish, price for all Bàn and Tủ
that cost more than VND25000
SELECT Product_Description, Product_Finish, Product_Price
FROM Product
WHERE (Product_Description like N'Bàn%' or Product_Description like N'Tủ%')
and (Product_Cost > 25000)
*Hình ảnh minh họa:
-- Query SQL 11: Which products in the Product view have a price between
VND1000000 and VND1500000?
SELECT Product_Description, Product_Price
FROM Product
WHERE Product_Price BETWEEN 1000000 and 1500000
*Hình ảnh minh họa:
-- Query SQL 13: Count the number of customers with addresses in each Zipcode
to which we ship
SELECT Customer_Zipcode, COUNT(Customer_Zipcode) as [Total]
FROM Customer
GROUP BY Customer_Zipcode
*Hình ảnh minh họa:
--INFORMATION SYSTEM
Query SQL 14: Find APPLICATIONS
only Zipcode with more –than
Personal Project
one customer 10
SELECT Customer_Zipcode, COUNT(Customer_Zipcode) as [Total]
FROM Customer
GROUP BY Customer_Zipcode
HAVING COUNT(Customer_Zipcode) > 1
*Hình ảnh minh họa:
-- Query SQL 15: What is total value of orders placed for each furniture
product
SELECT ol.Product_ID, SUM(Product_Price*Order_Quantity) as [Total]
FROM Order_Line ol, Product p
WHERE ol.Product_ID = p.Product_ID
GROUP BY ol.Product_ID
*Hình ảnh minh họa:
-- Query SQL 17: For each customer who had more than two orders, list the
customer_id and the total number of orders placed
-- Chèn thêm Order để test
INSERT INTO T_Order
VALUES(7,'08/23/2020','08/24/2020',106,'C01');
SELECT *FROM T_Order
*Hình ảnh minh họa:
-- Query SQL 18: What are the names of all customers who have placed orders?
-- Query SQL 19: List Customer_ID, name, Order_Number for all customers
listed in the CUSTOMER TABLE. Include the customer identification number and
name even if there is no order available for that customer
SELECT c.Customer_ID, Customer_Name, Order_Number
FROM Customer c left join T_Order o ON
c.Customer_ID = o.Customer_ID;
*Hình ảnh minh họa:
-- Query SQL 20: List Customer_ID, name, Order_Number for all orders listed
in the ORDER TABLE. Include the order number even if there is no order
available for that customer
SELECT c.Customer_ID, Customer_Name, Order_Number
FROM Customer c right join T_Order o ON
c.Customer_ID = o.Customer_ID;
-- Query SQL 23: Which customers have not placed any orders for "Bàn Ghế"
SELECT Customer_Name
FROM Customer
WHERE Customer_ID not in (SELECT Customer_ID
FROM Product p, Order_Line ol, T_Order o
-- Query SQL 24: What are the order_number for all orders that have included
furniture finished in "Đà Nẵng"
SELECT Order_Number
FROM T_Order
WHERE Order_Number in (SELECT o.Order_Number
FROM Product p, Order_Line ol, T_Order o
WHERE o.Order_Number = ol.Order_Number
and ol.Product_ID = p.Product_ID
and Product_Finish = N'Đà Nẵng')
*Hình ảnh minh họa:
-- Query SQL 25: List details about the product with the highest price
SELECT *
FROM Product
WHERE Product_Price = (SELECT MAX(Product_Price) FROM Product)
*Hình ảnh minh họa:
-- Câu 4: Cho biết thông tin thanh toán của hóa đơn khi người dùng nhập vào
Invoice Number
GO
CREATE PROC SP_OUTPUT_INVOICE
@Invoice_Number INT
AS
BEGIN
IF NOT EXISTS(SELECT Invoice_Number FROM Invoice WHERE
Invoice_Number = @Invoice_Number)
PRINT N'Invoice Number ' + CAST(@Invoice_Number AS VARCHAR)
+ N' không tồn tại.'
ELSE
SELECT I.*, HI.Payment_Total, P.Payment_Amount,
P.Payment_Date FROM Invoice I, Payment P, History_Invoice HI
-- Câu 5: Cho biết lãi suất bán hàng trong ngày khi người dùng nhập vào
Invoice Date
GO
CREATE PROC SP_OUTPUT_PAYMENT
@Invoice_Date DATE
AS
BEGIN
IF NOT EXISTS(SELECT Invoice_Date FROM Invoice WHERE Invoice_Date
= @Invoice_Date)
PRINT N'Invoice_Date ' + CAST(@Invoice_Date AS VARCHAR) +
N' không tồn tại.'
ELSE
INFORMATION SELECT
SYSTEM APPLICATIONS
Payment_Amount – Personal
AS [LAI Project
SUAT] FROM Payment P, Invoice 23
I
WHERE (I.Invoice_Number = P.Invoice_Number) AND
(I.Invoice_Date = @Invoice_Date)
END
EXEC SP_OUTPUT_PAYMENT '04/26/2020'
SELECT *FROM Payment
*Hình ảnh minh họa:
Bảng Invoice:
Bảng History_Invoice:
Bảng History_Invoice:
-- Câu 2: Viết Trigger khi khách hàng thanh toán tiền(PaymentAmount) thì số
tiền trong Payment_Total cũng sẽ giảm theo
GO
CREATE TRIGGER TR_UPDATE_PAYMENT_TOTAL ON Payment
FOR INSERT
ASINFORMATION SYSTEM APPLICATIONS – Personal Project 25
BEGIN
UPDATE History_Invoice
SET Payment_Total = Payment_Total - (SELECT Payment_Amount FROM
INSERTED)
WHERE Invoice_Number = (SELECT Invoice_Number FROM INSERTED)
END
V. Write Functions:
-- Câu 1: Viết hàm dùng để tính tổng số lượng khách đặt hàng trong ngày. Hiển
thị những ngày có tổng số lượng khách đặt hàng lớn hơn ngày hiện tại(Em dùng
ngày cụ thể để phù hợp với database)
-- Hàm tính tổng số lượng khách đặt hàng trong ngày
GO
CREATE FUNCTION F_LIST_CUSTOMER(@D DATE)
RETURNS INT
ASINFORMATION SYSTEM APPLICATIONS – Personal Project 26
BEGIN
RETURN (SELECT COUNT(Customer_ID) FROM T_Order WHERE
Order_Placement_Date = @D);
END
GO
PRINT DBO.F_LIST_CUSTOMER('01/01/2020')
*Hình ảnh minh họa:
-- Câu 2: Viết hàm để xem tổng số đầu mặt hàng cho các nhóm hàng khi người sử
INFORMATION
dụng SYSTEM
nhập vào product APPLICATIONS – Personal Project
line name 27
GO
CREATE FUNCTION F_SUM_PRODUCT(@N NVARCHAR(50))
RETURNS @KETQUA TABLE(NAMEP NVARCHAR(50), SOLUONG INT)
AS
BEGIN
INSERT INTO @KETQUA
SELECT P.Product_Line_Name, COUNT(Product_ID)
FROM Product P, Product_Line PL
WHERE (P.Product_Line_Name = PL.Product_Line_Name) AND
(P.Product_Line_Name = @N)
GROUP BY P.Product_Line_Name
RETURN;
END
*Hình ảnh minh họa: