Lab Assignment 6 Views
Lab Assignment 6 Views
CREATE VIEW
Assignment 6
SQL SCRIPT 1: Create a view that counts the number of hires for each month. Sort the list by year then
by month with its number of hires.
SQL SCRIPT 2: Formulate a view that counts that number of sales transactions a customer has entered.
You must display the customer number, customer name, and the total number of sales transactions.
Sort the list from highest to lowest count.
CREATE VIEW salesper AS
SELECT*FROM
ON customer.custNo=customer.custNo
GROUP BY customer.custNo,customer.custName
ORDER BY COUNT(transNo)DESC)cs ;
SQL SCRIPT 3: What is the average salary of each job position? Exclude manager, vice president, and
president from the generated list. Your view must display the job code, job description, and the average
salary of each job.
ON jb.jobCode = his.jobCode
SQL SCRIPT 4: Design a view will determine products that have undergone price changes. Sort the list
from highest to lowest count which also displays the product code and product description. Exclude
products that have no price change.
SELECT*FROM
FROM product pr
ON pr.prodCode = pp.prodCode
GROUP BY pr.prodCode,pr.description
HAVING COUNT(unitprice)>1
ORDER BY COUNT(unitPrice)DESC)pph;
SQL SCRIPT 5: What is the current price of each product? Create a view that displays the product code,
product description, unit, and its current price.
FROM product pr
ON pr.prodCode = c.prodCode
JOIN(SELECT h1.prodCode,MAX(h1.effdate)cDate
FROM priceHist h1
GROUP BY h1.prodCode)h
ON pr.prodCode = h.prodCode AND c.effDate=cDate;
SQL SCRIPT 6: Determine the current number of employees per each department. Your view should
contain the department code, department name, and the number of employees a department has. Do
not include separated (with SEPDATE values) employees.
FROM department q
ON o.deptCode = q.deptCode
ON k.empNo = o.empNo
MAX (i2.effDate) AS gw
FROM jobHistory i2
GROUP BY i2.empNo) aa
ON o.empNo = aa.empNO
AND o.effDate = gw
SQL SCRIPT 7: Who among the employees received the most number of promotions? The view must
contain the employee number, employee name (combined last name and first name). Sort the list with
the most number of promotions. Do not include separated employees.
SELECT * FROM
FROM employee a
LEFT JOIN jobHistory e
ON a.empNo = e.empNo
ORDER BY COUNT(e.jobCode)DESC)eg;
SQL SCRIPT 8: What is the most bought product of the company? Your view must list the highest to
lowest including the product code, product description, and unit, total_quantity.
SELECT * FROM
FROM product p
ON p.prodCode=d.prodCode
GROUP BY p.prodCode, p.description, p.unit
SQL SCRIPT 9: What is the total sales of each product? Your view must contain product code,
description, unit, total sales.
To generate the correct result you must consider on how to get the sales of each product (unit price *
quantity). But what unit price do you use if the product has more than one unit price?
The answer to this is to get the sales date of the transaction first and compare it with the unit price that
is aligned with its effectivity date. Meaning, if the sales date 6/27/2010 you will use the unit price
effective date of 5/15/2010 and NOT the 08/01/2010; if sales date is 03/21/2011, then the unit price to
be used must be 08/01/2010 but NOT the 05/15/2010.
HINT: On your first view, get first the transno, salesdate, effectivity date, prodcode, quantity. Filter
records that salesdate must be greater than or equal to effectivity date.
On your second view, use the first view to get the MAX() effective date for each transaction. This is
necessary to avoid getting two dates if the sales date is 03/21/2011 but the product has two effectivity
dates (05/1/2010 and 08/01/2010).
On your last view, relate the product table to your second view and to the price history table and
generate the total sales of each product.
SQL SCRIPT 10: Who is the customer that contributed the sales of the company? Sort your list from
highest to lowest including the customer code, name, and its total sales.
-- Fictitious Company
lastname VARCHAR(15),
firstname VARCHAR(15),
birthdate DATE,
hiredate DATE,
sepDate DATE) ORGANIZE BY ROW;
-- Create DEPARTMENT
('EXC', 'Executive'),
('PAY', 'Payroll'),
('WHS', 'Warehouse');
-- Create JOB
jobDesc VARCHAR(20))
ORGANIZE BY ROW;
-- Create jobHistory
custname
VARCHAR(20),
address
VARCHAR(50),
payterm
VARCHAR(3) CONSTRAINT pay_ck
CHECK
(payterm IN ('COD', '30D', '45D'))) ORGANIZE BY ROW;
INSERT INTO customer VALUES('C0011','B&G Foods', '4 Gatehall Dr., Ste. 110
Parsippany, NJ 07054', '45D');
INSERT INTO customer VALUES('C0012','Dexter Santos', '2739 Rebeiro Ave,
Santa Clara, CA 95051', 'COD');
INSERT INTO customer VALUES('C0043','Nea Sanchez', '371 7th Ave, New York,
NY 10001', 'COD');
INSERT INTO customer VALUES('C0079','Social Sec Admn NY', '123 William St,
New York, NY 10038', '45D');
salesDate DATE,
custNo VARCHAR(5),
empNo
VARCHAR(5),
ORGANIZE BY ROW;
-- Create PRODUCT
description
VARCHAR(30),
unit
VARCHAR(3) CONSTRAINT unit_ck
CHECK (unit
IN ('pc','ea','mtr','pkg','ltr')))
ORGANIZE BY
ROW;
-- Create salesDETAIL
payDate
DATE,
amount
DECIMAL(10,2),
transno
VARCHAR(8) REFERENCES sales)
ORGANIZE
BY ROW;
-- Insert PAYMENT rows
prodCode
VARCHAR(6) NOT NULL REFERENCES product,
CHECK
(unitPrice > 0),
PRIMARY
KEY (effDate, prodCode));
HopeDB.sql