MYSQL 8 und SUM and Join erzeugt falsche Ergebnisse
Hallo zusammen,
ich versuche mittels eines Select eine Aggregierung einer Datenbanktabelle zu erzeugen.
Dabei ist jedoch auffällig, dass in unterschiedlichen Zeiträumen unterschiedliche Ergebnisse herauskommen, auch wenn sich an den darunter liegenden Daten nichts geändert hat.
Ich verwende hierfür das folgende Statement:
Hat jemand eine Idee, woher dieses Verhalten kommt?
ich versuche mittels eines Select eine Aggregierung einer Datenbanktabelle zu erzeugen.
Dabei ist jedoch auffällig, dass in unterschiedlichen Zeiträumen unterschiedliche Ergebnisse herauskommen, auch wenn sich an den darunter liegenden Daten nichts geändert hat.
Ich verwende hierfür das folgende Statement:
SELECT
`03_table`.`accounting`.`Year` AS `Year`,
`03_table`.`accounting_class`.`DE_FinancialName` AS `DE_FinancialName`,
SUM( (CASE
WHEN (`03_table`.`accounting_type`.`Accounting_Type_Short_Code` = 'IN') THEN `03_table`.`accounting`.`Amount`
END)) AS `IN`,
SUM( (CASE
WHEN (`03_table`.`accounting_type`.`Accounting_Type_Short_Code` = 'OU') THEN `03_table`.`accounting`.`Amount`
END)) AS `OU`,
SUM( (CASE
WHEN (`03_table`.`accounting_type`.`Accounting_Type_Short_Code` = 'PR') THEN `03_table`.`accounting`.`Amount`
END)) AS `PR`,
SUM((CASE
WHEN (`03_table`.`accounting_type`.`Accounting_Type_Short_Code` = 'CR') THEN `03_table`.`accounting`.`Amount`
END)) AS `CR`
FROM
((((`03_table`.`accounting`
)
LEFT JOIN `03_table`.`accounting_type` ON ((`03_table`.`accounting`.`Accounting_Type` = `03_table`.`accounting_type`.`UUID`)))
LEFT JOIN `03_table`.`accounting_class` ON ((`03_table`.`accounting`.`Accounting_Class` = `03_table`.`accounting_class`.`UUID`)))
)
GROUP BY `03_table`.`accounting`.`Year` , `03_table`.`accounting_class`.`DE_FinancialName`
ORDER BY `03_table`.`accounting`.`Year` DESC
Hat jemand eine Idee, woher dieses Verhalten kommt?
Please also mark the comments that contributed to the solution of the article
Content-Key: 591204
Url: https://administrator.de/contentid/591204
Printed on: April 25, 2024 at 16:04 o'clock
2 Comments
Latest comment
Hallo Loderich,
die Query sieht für mich erst einmal stimmig aus, auch wenn ich aus der MS SQL Ecke komme.
Ich würde mal prüfen ob 03_table.accounting_type.Account.Accounting_Typ_Short_Code noch anderen Werte
außer den in der Query abgefragten gibt. Falls es NULL Werte gibt, wäre das eine Fehlerquelle, da du diese in den CASE Abfragen nicht abfängst.
Grüße vom it-frosch
die Query sieht für mich erst einmal stimmig aus, auch wenn ich aus der MS SQL Ecke komme.
Ich würde mal prüfen ob 03_table.accounting_type.Account.Accounting_Typ_Short_Code noch anderen Werte
außer den in der Query abgefragten gibt. Falls es NULL Werte gibt, wäre das eine Fehlerquelle, da du diese in den CASE Abfragen nicht abfängst.
Grüße vom it-frosch