You are not logged in.

  • Login

1

Friday, May 20th 2011, 9:44am

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:

SQL Code

1
2
3
4
5
6
7
8
9
SELECT Religion.Name, ((SUM(Anteil.Anteil) / SUM(Country.Population) ) * 100) Prozentanteil
FROM Religion INNER JOIN ( 
 
    SELECT Country.Code AS Country, Religion.Name AS Name, SUM((Religion.Percentage * Country.Population) / 100) AS Anteil
    FROM Country INNER JOIN Religion ON Country.Code = Religion.Country
    GROUP BY Country.Code, Religion.Name 
 
) Anteil ON Religion.Name = Anteil.Name AND Religion.Country = Anteil.Country, Country
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?

Source code

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


Kann mir da einer helfen und irgendwie einen Tipp geben?

2

Saturday, May 21st 2011, 11:26am

Hi!

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

SQL Code

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

3

Saturday, May 21st 2011, 2:49pm

Ah, vielen Dank! Ich hab das jetzt angepasst und bekomme reale Werte die nicht über 100% liegen ;).

SQL Code

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


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?

4

Saturday, May 21st 2011, 3:41pm

Stimmt. Bei "Insgesamt" kannst du entweder die Weltbevölkerung nehmen, oder die "Weltbevölkerung, die eine Religion angegeben hat"
Die Differenz dazwischen ist die ohne konkrete Angabe.

Social bookmarks