-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejer3.3.sql
55 lines (36 loc) · 1.51 KB
/
ejer3.3.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
48
49
50
51
DROP TABLE IF EXISTS stats;
CREATE TABLE stats (
countrycode character(3) NOT NULL PRIMARY KEY REFERENCES country(code),
cant_lenguas integer,
pop_urbana integer
);
SELECT * FROM stats;
INSERT INTO stats (countrycode, cant_lenguas)
SELECT country.code, COUNT(language) AS cant_lenguas
FROM country
INNER JOIN countryLanguage ON country.code = countryLanguage.countrycode
GROUP BY country.code
ORDER BY country.code ASC;
UPDATE stats SET pop_urbana = subquery.pop_urbana
FROM(
SELECT country.code AS countrycode, SUM(city.population) AS pop_urbana
FROM country
INNER JOIN city ON country.code = city.countrycode
GROUP BY country.code
ORDER BY country.code ASC
) AS subquery
WHERE stats.countrycode = subquery.countrycode;
-- _______________________________________________________________________________________________________
-- Da resultados raros, parecieran duplicados o mas
SELECT country.code, COUNT(countrylanguage.language) AS cant_lenguas, COALESCE(SUM(city.population), 0) AS pop_urbana
FROM country
INNER JOIN city ON country.code = city.countrycode
INNER JOIN countryLanguage ON country.code = countryLanguage.countrycode
GROUP BY country.code
ORDER BY country.code;
SELECT country.code AS countrycode, COUNT(countrylanguage.language) AS cant_lenguas, COALESCE(SUM(city.population), 0) AS pop_urbana
FROM country
LEFT JOIN countrylanguage ON country.code = countrylanguage.countrycode
LEFT JOIN city ON country.code = city.countrycode
GROUP BY country.code
ORDER BY country.code;