-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWayfair_Company_Export_Import.sql
More file actions
36 lines (33 loc) · 1.44 KB
/
Wayfair_Company_Export_Import.sql
File metadata and controls
36 lines (33 loc) · 1.44 KB
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
create table if not exists Companies(name varchar(100), nation varchar(100), PRIMARY KEY (name));
insert into Companies(name,nation) values ('Alice','Wonderland');
insert into Companies(name,nation) values ('Y-zap','Wonderland');
insert into Companies(name,nation) values ('Absolute','Mathland');
insert into Companies(name,nation) values ('Arcus','Mathland');
insert into Companies(name,nation) values ('Li','Underwater');
insert into Companies(name,nation) values ('None at all','Nothingland');
create table if not exists Trade(id integer, seller varchar(100), buyer varchar(100), value integer);
insert into Trade(id,seller,buyer,value) values (20121107,'Li','Alice',10);
insert into Trade(id,seller,buyer,value) values (20123112,'Arcus','Y-zap',30);
insert into Trade(id,seller,buyer,value) values (20120125,'Alice','Arcus',100);
insert into Trade(id,seller,buyer,value) values (20120216,'Li','Absolute',30);
insert into Trade(id,seller,buyer,value) values (20120217,'Li','Absolute',50);
select table1.country, table1.export, table2.import from
(select c1.nation as country, coalesce(sum(t1.value),0) as export from
Companies c1 left join Trade t1
on c1.name=t1.seller
group by 1
) table1
join
(
select c2.nation as country, coalesce(sum(t2.value),0) as import from
Companies c2 left join Trade t2
on c2.name=t2.buyer
group by 1
) table2
on table1.country=table2.country
Answer:
Country export import
Mathland 30 180
Nothingland 0 0
Underwater 90 0
Wonderland 100 40