-
Notifications
You must be signed in to change notification settings - Fork 0
/
35_intl_vs_domestic_salesMySQL.sql
47 lines (40 loc) · 1.09 KB
/
35_intl_vs_domestic_salesMySQL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use twitterdb
DROP TABLE IF EXISTS sales CASCADE;
CREATE TABLE sales(sales_rep_id int, invoice_id text, amount int, sales_type text);
INSERT INTO sales VALUES
(1001,'Inv345756',13454,'International'),
(2002,'Inv546744',3434,'International'),
(4004,'Inv234745',54645,'International'),
(5005,'Inv895745',234345,'International'),
(7007,'Inv006321',776,'International'),
(1001,'Inv734534',4564,'Domestic'),
(2002,'Inv600213',34534,'Domestic'),
(3003,'Inv757853',345,'Domestic'),
(6006,'Inv198632',6543,'Domestic'),
(8008,'Inv977654',67,'Domestic');
WITH cte_international AS (
SELECT *
FROM sales
WHERE sales_type = 'International'
),
cte_domestic AS (
SELECT *
FROM sales
WHERE sales_type = 'Domestic'
),
outer_join AS (
SELECT d1.sales_rep_id
FROM cte_domestic AS d1
LEFT JOIN cte_international AS i1
ON d1.sales_rep_id = i1.sales_rep_id
WHERE i1.sales_rep_id IS NULL
UNION
SELECT i1.sales_rep_id
FROM cte_domestic AS d1
RIGHT JOIN cte_international AS i1
ON d1.sales_rep_id = i1.sales_rep_id
WHERE d1.sales_rep_id IS NULL
)
SELECT *
FROM outer_join
ORDER BY sales_rep_id;