Skip to content

Instantly share code, notes, and snippets.

@baruchel
Created August 26, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baruchel/9f584388dcef84ea4d0c to your computer and use it in GitHub Desktop.
Save baruchel/9f584388dcef84ea4d0c to your computer and use it in GitHub Desktop.
-- Reproduire la table `CountryLanguage` à la différence près que la colonne Percentage doit maintenant contenir le pourcentage de locuteurs d'une langue dans un pays par rapport au nombre total de locuteurs de cette langue dans le monde.
SELECT CountryCode, Language, IsOfficial,
(A.Percentage * A.Population / B.nbr) AS Percentage
FROM (
SELECT CountryLanguage.*, Country.Population
FROM CountryLanguage
LEFT JOIN Country
ON CountryLanguage.CountryCode = Country.Code
) AS A
LEFT JOIN (
SELECT Language, SUM(Percentage * Country.Population)/100 AS nbr
FROM CountryLanguage
LEFT JOIN Country
ON CountryLanguage.CountryCode = Country.Code
GROUP BY Language
) AS B
USING(Language);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment