Mailinglisten-Archive |
> ich habe folgende Tabelle:
>
> auftrag baugruppe zeit
> xxxxx xxxx 1
> xxxxx xxxx 2
> xxxxx xxxx 34
> xxxxx xxxx 45
>
> durch eine Abfrage möchte ich folgende tabelle erhalten:
>
> auftrag baugruppe teile gesamtteile
> xxxxx xxxx 1 1
> xxxxx xxxx 2 3
> xxxxx xxxx 34 37
> xxxxx xxxx 45 82
>
> ich suche nach der funktion um die letzte spalte zu erzeugen
> meine versuche mit sum führten leider nicht zu dem gewünschten ergebniss.
>
du willst die letzte spalte tatsächlich so in die datenbank schreiben,
oder willst es dir nur so ausgeben lassen?
SELECT *,
SUM(table_copy.teile) as gesamtteile
FROM table
LEFT JOIN table AS table_copy
ON table.[ORDERBYFIELD] <= table_copy.[ORDERBYFIELD]
GROUP BY [ORDERBYFIELD]
ORDER BY [ORDERBYFIELD] ASC
bei einer sortierung nach auftrag also so:
SELECT *,
SUM(table_copy.teile) as gesamtteile
FROM table
LEFT JOIN table AS table_copy
ON table.auftrag <= table_copy.auftrag
GROUP BY auftrag
ORDER BY auftrag
alerdings muss das OORDERBYFIELD unique sein für ein korrektes ergebnis
also bruachst du bestimmt ein ORDER BY auftrag,baugruppe, dann siehts
(etwas komplizierte) so aus:
SELECT *,
SUM(table_copy.teile) as gesamtteile
FROM table
LEFT JOIN table AS table_copy
ON table.auftrag > table_copy.auftrag
OR (table.auftrag = table_copy.auftrag
AND table.baugruppe >= table_copy.baugruppe)
GROUP BY auftrag, baugruppe
ORDER BY auftrag, baugruppe
ABER *VORSICHIT* !! kann bei großen tabellen extrem lange dauern!
außerdem hab ichs nicht probiert, kann als sein das noch ein denkfehler
drin ist, aber grundsätzlich müsste es so in der weiße gehen
--
Sebastian Mendel
www.sebastianmendel.de
www.tekkno4u.de
www.nofetish.com
--
Infos zur Mailingliste, zur Teilnahme und zum An- und Abmelden unter
-->> http://www.4t2.com/mysql
php::bar PHP Wiki - Listenarchive