WCM - Das österreichische Computer Magazin Forenübersicht
 

Zurück   WCM Forum > Rat & Tat > Programmierung

Programmierung Rat & Tat für Programmierer

Microsoft KARRIERECAMPUS

Antwort
 
Themen-Optionen Ansicht
Alt 29.01.2005, 19:32   #1
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard [SQL] Statistik

Kann man mit SQL (MySQL) zwei Summen (SUM) dividieren?
Ich möchte den gewichteten Durchschnitt vom Wert w1 mit w2 gewichtet berechnen.
Das wäre
SUM(w1*w2)/SUM(w2).
SELECT SUM(w1*w2)/SUM(w2) FROM ... geht aber nicht. Geht das überhaupt oder muß ich das im Programm machen?

Außerdem würde ich gerne den Durchschnitt aus Summen berechnen, etwas in der Art:
sums=SELECT SUM(w1) FROM table GROUP BY w2
SELECT AVG(sums);
Das funktion iert in der Form klarerweise auch nicht. hat wer eine Idee wie das gehen könnte?

Jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Alt 29.01.2005, 23:34   #2
T.dot
Master
 
Registriert seit: 13.08.2003
Beiträge: 624


Standard

Hi!

Ich kann nicht dafür garantieren, das funktioniert - einfach probieren, kommt auch auf den sql-dialekt an, den du verwendest

select sum(w1) as Bla, sum(w2) as Blub, Bla/Blub as DasWasDuHabenWillst From ...

oder vl.

Select (Select sum(w1) from ...)/(Select sum(w2) from ...) as DasWasDuHabenWillst from ...

Gleiches fürs zweite:

Select avg(Select sum(w1) from ...) from ...

Wobei mysql Unterabfragen erst ab einer bestimmten Version unterstützt (bei mir gehts auf jeden Fall schon)...

Sonst musst es wohl oder übel über einen Zwischenschritt machen - vl. gehts auch über eine Stored-Procedure - damit kenn ich mich aber wirklich zu wenig aus...

mfg T.
T.dot ist offline   Mit Zitat antworten
Alt 30.01.2005, 12:11   #3
jak
Inventar
 
Registriert seit: 13.06.2001
Beiträge: 1.830


Standard

Ich verwend MySQL 4.0.21.
Werd' wohl updaten müssen.

Deine Vorschläge enden alle in Fehlermeldungen:
SELECT SUM(w1*w2) AS s1, SUM(w2) AS s2, s1 / s2 FROM table

#1054 - Unknown column 's1' in 'field list'

SELECT (SELECT SUM(w1*w2) FROM table) / (SELECT SUM(w2) FROM table ) AS test
FROM table

#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT SUM( w1*w2 ) FROM table ) / ( SELECT SU



#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT SUM(w1) FROM table GROUP BY w2) FROM ta

Man kann die Probleme aber mit 2 SQL-Statements lösen:
SELECT @s1 := SUM(w1*w2) , @s2 := SUM(w2) FROM table;
SELECT @s1 / @s2 FROM table LIMIT 1

SELECT @s1:=SUM( hours )
FROM courses
GROUP BY semester ;
SELECT AVG(@s1) FROM courses;

Find' ich aber nicht besonders schön, vor allem weil das 2. Falsche Werte liefert.
Jak
____________________________________
Join the DNRC | Godwin\'s Law (thx@stona)
Documentation is like sex: If it\'s good, it\'s very, very good. If it\'s bad, it\'s better than nothing.
\"In theory, theory and practice are the same. In practice, they are not\" (Lawrence Berra)
jak ist offline   Mit Zitat antworten
Antwort


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.

Gehe zu


Alle Zeitangaben in WEZ +2. Es ist jetzt 12:51 Uhr.


Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Forum SEO by Zoints
© 2009 FSL Verlag