Skip to content

Commit

Permalink
Create 2.3_FINAL.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannidis authored Apr 28, 2018
1 parent 3b0ef0e commit 67892d1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 2.3_FINAL.sql
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);

0 comments on commit 67892d1

Please sign in to comment.