-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
DROP VIEW IF EXISTS SALES; | ||
CREATE VIEW SALES AS ( | ||
SELECT | ||
A.salesman_id, | ||
sum(A.price) AS sale | ||
FROM sales_history AS A | ||
WHERE A.action = 'sale' | ||
GROUP BY A.salesman_id | ||
); | ||
|
||
DROP VIEW IF EXISTS BUYS; | ||
CREATE VIEW BUYS AS ( | ||
SELECT | ||
A.salesman_id, | ||
sum(A.price) AS buy | ||
FROM sales_history AS A | ||
WHERE A.action = 'buy' | ||
GROUP BY A.salesman_id | ||
); | ||
|
||
DROP VIEW IF EXISTS PROFIT; | ||
CREATE VIEW PROFIT AS ( | ||
SELECT | ||
SALES.salesman_id, | ||
(SALES.sale - BUYS.buy) AS profit | ||
FROM SALES NATURAL JOIN BUYS | ||
); | ||
|
||
SELECT salesman_id, employee.first_name, employee.last_name, profit | ||
FROM PROFIT INNER JOIN employee ON PROFIT.salesman_id = employee.id | ||
WHERE profit = (SELECT max(profit) FROM PROFIT); |