Laden...

MySQL Query mit "komischem" GROUP

Erstellt von TheBrainiac vor 8 Jahren Letzter Beitrag vor 8 Jahren 826 Views
TheBrainiac Themenstarter:in
795 Beiträge seit 2006
vor 8 Jahren
MySQL Query mit "komischem" GROUP

verwendetes Datenbanksystem: MySQL

Hi @ All.

Ich habe folgende Tabellenstruktur für Buchungen:

| account | date       | month   | test | ...
| 1       | 2015-01-01 | 2015-01 | 1    | ...
| 1       | 2015-01-02 | 2015-01 | 1    | ...
| 1       | 2015-03-03 | 2015-01 | 1    | ...
| 1       | 2015-02-01 | 2015-02 | 2    | ...
| 1       | 2015-03-01 | 2015-03 | 4    | ...
| 1       | 2015-03-02 | 2015-03 | 4    | ...
| 2       | ...        | ...     | ...  | ...

Ich brauche ein Query, das so gruppiert, dass ich pro account eine Zeile habe mit Summen der übrigen Spalten. Allerdings darf pro month nur die Zeile mit dem höchsten date mit in die Summe einbezogen werden. Das entsprechende date muss nicht zwingend im jeweiligen month liegen.

So soll zum Beispiel hier folgendes raus kommen:

| account | test | ...
| 1       | 7    | ...
| 2       | ...  | ...

Was ich bräuchte wäre so etwas wie folgendes. Nur bin ich mir nicht sicher, wie ich die HAVING-Klausel Formulieren muss, bzw. ob das mit HAVING überhaupt realisierbar ist.

SELECT SUM(test)
FROM bookings
GROUP BY account, month
HAVING date = MAX(date)

LG, Christian.

Test-Umgebung:

CREATE TABLE `bookings` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `account` int(11) DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  `month` date DEFAULT NULL,
  `test` int(11) DEFAULT NULL,
  PRIMARY KEY (`ID`)
);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (1,1,'2015-01-01 00:00:00','2015-01-01',1);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (2,1,'2015-03-02 00:00:00','2015-03-01',4);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (3,1,'2015-01-02 00:00:00','2015-01-01',1);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (4,1,'2015-03-01 00:00:00','2015-01-01',1);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (5,1,'2015-02-01 00:00:00','2015-02-01',2);
INSERT INTO `bookings` (`ID`, `account`, `date`, `month`, `test`) VALUES (6,1,'2015-03-01 00:00:00','2015-03-01',4);
`There are 10 types of people in the world: Those, who think they understand the binary system Those who don't even have heard about it And those who understand "Every base is base 10"`
2.298 Beiträge seit 2010
vor 8 Jahren

Ich wäre jetzt ganz stumpf einfach auf ein SubSelect gegangen und hätte mir pro Account das höchste Datum geholt. Ist das eventuell eine Idee für dich?

Durch den aktuellen Weg "Having date = Max(date)" schließt du per se jeden Eintrag aus, der nicht vom aktuellsten Datum ist unabhängig vom Account.

Wissen ist nicht alles. Man muss es auch anwenden können.

PS Fritz!Box API - TR-064 Schnittstelle | PS EventLogManager |

TheBrainiac Themenstarter:in
795 Beiträge seit 2006
vor 8 Jahren
SELECT b.`account`, SUM(b.`test`)
FROM `bookings` AS b
INNER JOIN (SELECT `account`, `month`, MAX(`date`) AS `date`
	FROM `bookings`
	GROUP BY `account`, `month`) AS t
	ON t.`account` = b.`account`
    AND t.`month` = b.`month`
    AND t.`date` = b.`date`
GROUP BY b.`account`

Danke!

`There are 10 types of people in the world: Those, who think they understand the binary system Those who don't even have heard about it And those who understand "Every base is base 10"`