0% found this document useful (0 votes)
4 views

New Microsoft Word Document

The document outlines various SQL commands for managing a sales database, including creating tables, calculating profit percentages, updating discount rates based on inventory, and creating views for client and sales data. It also includes constraints for ensuring non-negative amounts in client records and modifying existing views. The commands demonstrate how to manipulate and query data effectively within the database structure.

Uploaded by

thanhcongcsdp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

New Microsoft Word Document

The document outlines various SQL commands for managing a sales database, including creating tables, calculating profit percentages, updating discount rates based on inventory, and creating views for client and sales data. It also includes constraints for ensuring non-negative amounts in client records and modifying existing views. The commands demonstrate how to manipulate and query data effectively within the database structure.

Uploaded by

thanhcongcsdp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

use SalesManagement_Lab5;

-- LAB 6

-- 2. Create a separate table name as “ProductCost” from “Product” table,

-- which contains the information about product name and its buying price.

CREATE Temporary TABLE ProductCost AS

SELECT Product_Name, Cost_Price

FROM Product;

-- 3. Compute the profit percentage for all products.

-- Note: profit = (sell-cost)/cost*100

alter table Product

add Profit_Percentage decimal(15,2);

SELECT Product_Name,

round(((Sell_Price - Cost_Price / Cost_Price) * 100,2)) AS Profit_Percentage

FROM Product;

-- 4. If a salesman exceeded his sales target by more than equal to 75%,

-- his remarks should be ‘Good’.

-- 5. If a salesman does not reach more than 75% of his sales objective, he is labeled as 'Average'.

-- 6. If a salesman does not meet more than half of his sales objective, he is considered 'Poor'.

SELECT Salesman_Name, Target_Achieved, Sales_Target,

ROUND((Target_Achieved / Sales_Target) * 100, 2) AS Achievement_Percentage,

CASE

WHEN ROUND((Target_Achieved / Sales_Target) * 100, 2) >= 75.00 THEN 'Good'

WHEN 50.00 <= ROUND((Target_Achieved / Sales_Target) * 100, 2) < 75.00 THEN 'Average'

ELSE 'Poor'
END AS Remarks

FROM Salesman;

-- 7. Find the total quantity for each product. (Query)

select Product_Name, sum(Quantity_On_Hand) as Quantity

from Product

group by Product_Number, Product_Name ;

-- 8. Add a new column and find the total quantity for each product.

alter table Product

add Total_Quantity int;

-- 9. If the Quantity on hand for

-- each product is more than 10, change the discount rate to 10 otherwise set to 5.

UPDATE Product

SET Total_Quantity = (

SELECT SUM(Quantity_On_Hand)

FROM SalesOrderDetails

WHERE SalesOrderDetails.Product_Number = Product.Product_Number

);

alter table Product

add Discount_Rate decimal(15,2);

UPDATE Product

SET Discount_Rate =

CASE

WHEN Quantity_On_Hand > 10 THEN 10

ELSE 5

END;
-- 10. If the Quantity on hand for each product is more than equal to 20,

-- change the discount rate to 10, if it is between 10 and 20 then change to 5,

-- if it is more than 5 then change to 3 otherwise set to 0.

UPDATE Product

SET Discount_Rate =

CASE

WHEN Quantity_On_Hand >= 20 THEN 10

WHEN Quantity_On_Hand BETWEEN 10 AND 19 THEN 5

WHEN Quantity_On_Hand > 5 THEN 3

ELSE 0

END;

-- 11. The first number of pin code in the client table should start with 7.

SELECT Client_Name, Pincode

FROM Clients

WHERE Pincode NOT LIKE '7%';

-- 1. Creating a view

-- CREATE [OR REPLACE] VIEW view_name

-- AS

-- select-statement;

-- OR REPLACE - added to overwrite the old view with the same name if applicable.

-- 2. Changing a view

-- ALTER VIEW view_name AS select_statement;

-- 3. Renaming a view

-- RENAME TABLE view_name TO new_view_name;

-- 4. Deleting a view

-- DROP VIEW [IF EXISTS] view_name;

-- 12. Creates a view name as clients_view that shows all customers information from Thu Dau Mot.

create view clients_view as


select * from clients;

-- 13. Drop the “client_view”.

drop view clients_view;

-- 14. Creates a view name as clients_order

-- that shows all clients and their order details from Thu Dau Mot.

CREATE VIEW clients_order AS

SELECT DISTINCT

c.Client_Name,

c.Client_Number,

sod.Order_Number

FROM clients c

JOIN salesorder so ON so.Client_Number = c.Client_Number

JOIN salesorderdetails sod ON sod.Order_Number = so.Order_Number

WHERE c.City = 'Thu Dau Mot';

-- 15. Creates a view that selects every

-- product in the "Products" table with a sell price higher than the average sell price.

create view High_Sales_Product as

select p.Product_Name, p.Product_Number

from Product p

where p.Sell_Price > ( select avg(Sell_Price) from Product) ;

-- 16. Creates a view name as salesman_view that show all salesman

-- information and products (product names, product price, quantity order) were sold by them.

CREATE VIEW salesman_view AS

SELECT DISTINCT

sm.Salesman_Number,

sm.Salesman_Name,

sm.City,
p.Product_Name,

p.Sell_Price,

sod.Order_Quantity

FROM Salesman sm

JOIN SalesOrder so ON so.Salesman_Number = sm.Salesman_Number

JOIN SalesOrderDetails sod ON sod.Order_Number = so.Order_Number

JOIN Product p ON p.Product_Number = sod.Product_Number;

-- 17. Creates a view name as sale_view that show all salesman information and product

-- (product names, product price, quantity order) were sold by them with order_status = 'Successful'.

create view sale_view as

select sm.Salesman_Name,

sm.Salesman_Number,

p.Product_Name,

p.Sell_Price,

sod.Order_Quantity

from Salesman sm

JOIN SalesOrder so ON so.Salesman_Number = sm.Salesman_Number

JOIN SalesOrderDetails sod ON sod.Order_Number = so.Order_Number

JOIN Product p ON p.Product_Number = sod.Product_Number

where so.Order_Status = 'Successful' ;

-- 18. Creates a view name as sale_amount_view that show all salesman information and sum order

-- quantity of product greater than and equal 20 pieces were sold by them with order_status =
'Successful'.

create view sale_amount as

select sm.Salesman_Name,

sm.Salesman_Number,
sum(sod.Order_Quantity) >= 20

from Salesman sm

JOIN SalesOrder so ON so.Salesman_Number = sm.Salesman_Number

JOIN SalesOrderDetails sod ON sod.Order_Number = so.Order_Number

where so.Order_Status = 'Successful' ;

-- 19. Amount paid and amounted due should not be negative when you are inserting the data.

alter table clients

add constraint chk_amount_paid check (Amount_Paid >= 0 ),

add constraint chk_amount_due check (Amount_Due >= 0 );

-- 20. Do not enforce the check constraint for pincode.

alter table clients

drop constraint chk_pincode;

-- 21. How to alter a check constraint enforcement state?

alter table clients

You might also like