SQL Abfrage mit SUM und GROUP BY

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • SQL Abfrage mit SUM und GROUP BY

    Hallo,

    leider habe ich wieder ein Problem und irgendwie verliere ich manchmal den totalen Überblick bei SQL :(.

    Ich soll den prozentualen Anteil von jeder Religion an der Weltbevölkerung berechnen. Also ich versteh es so das am Ende eine Tabelle rauskommen soll wo Religion und der Anteil an der Weltbevölkerung in % steht.

    Ich habe jetzt einiges rumprobiert in dem ich zuerst einmal Bevölkerungsanzahl zu jeder Religion zu jedem Land berechne und dann das als UNterabfrage benutze aber es kommt nicht wirklich das raus was ich brauche:

    Quellcode

    1. SELECT Religion.Name, ((SUM(Anteil.Anteil) / SUM(Country.Population) ) * 100) Prozentanteil
    2. FROM Religion INNER JOIN (
    3. SELECT Country.Code AS Country, Religion.Name AS Name, SUM((Religion.Percentage * Country.Population) / 100) AS Anteil
    4. FROM Country INNER JOIN Religion ON Country.Code = Religion.Country
    5. GROUP BY Country.Code, Religion.Name
    6. ) Anteil ON Religion.Name = Anteil.Name AND Religion.Country = Anteil.Country, Country
    7. GROUP BY Religion.Name;


    Hier ist ein kleiner Ausschnitt von den ersten 11 Einträgen (von insgesamt ca.70) welche aber schon 100% übersteigt - also kann meine SQL Abfrage ja nicht richtig sein?

    Quellcode

    1. Alawite
    2. Albanian Orthodox 2,20112286373692313796133576149372737665
    3. Anglican 2,66761970363301366437320801865885484664
    4. Armenian Apostolic 0,7072306690370996329339221270640916907084
    5. Armenian Orthodox 5,81306760636146146789629358111805226717
    6. Bahai 0,7463724112340413946470831185349149585934
    7. Baptist 0,1898130219851875104720970874743909029647
    8. Buddhism 65,63578858972870627201455004660773897587
    9. Buddhist 25,88433044562024875297721117586038893035
    10. Bulgarian Orthodox 24,79747831367122871695381674202328908582
    11. Calvinist 6,7762081028821160152443349152227636294
    Alles anzeigen


    Kann mir da einer helfen und irgendwie einen Tipp geben?
  • Hi!

    Du musst Religion (GROUP BY Religion) mit Insgesamt (Ohne GROUP) vergleichen

    Quellcode

    1. SELECT
    2. Anteil.Name,
    3. Anteil.Zahl,
    4. Insgesamt.Zahl
    5. FROM (
    6. SELECT Religion.Name AS Name,
    7. SUM((Religion.Percentage * Country.Population) / 100) AS Zahl
    8. FROM Country
    9. INNER JOIN Religion ON Country.Code = Religion.Country
    10. GROUP BY Religion.Name
    11. ) Anteil,
    12. (
    13. SELECT SUM((Religion.Percentage * Country.Population) / 100) AS Zahl
    14. FROM Country
    15. INNER JOIN Religion ON Country.Code = Religion.Country
    16. ) Insgesamt
    Alles anzeigen
  • Ah, vielen Dank! Ich hab das jetzt angepasst und bekomme reale Werte die nicht über 100% liegen ;).

    Quellcode

    1. SELECT Anteil.Name, ((SUM(Anteil.Anteil) / SUM(Insgesamt.Insgesamt) ) * 100) Prozentanteil
    2. FROM (
    3. SELECT Religion.Name AS Name, SUM((Religion.Percentage * Country.Population) / 100) AS Anteil
    4. FROM Country
    5. INNER JOIN Religion ON Country.Code = Religion.Country
    6. GROUP BY Religion.Name
    7. ) Anteil,
    8. (
    9. SELECT SUM((Religion.Percentage * Country.Population) / 100) AS Insgesamt
    10. FROM Country
    11. INNER JOIN Religion ON Country.Code = Religion.Country
    12. ) Insgesamt
    13. GROUP BY Anteil.Name;
    Alles anzeigen


    Aber eine Frage habe ich noch: Warum summiere ich alle Anhänger jede Religion und definiere das dann als Weltbevölkerung? Müsste ich nicht nach den Summierung der Einwohneranzahl der Lände gehen?