2016-08-01 11 views
2

enter image description hereMYSQL Abfrage die Summe und die letzten Datensatz Wert in GROUP BY

enter image description here

Ich habe zwei Tabelle 1 ist Konto und der andere ist account_spend_history AS ash Konto ist das Eltern/Master-Tabelle und das bekommen Schien eine Untertabelle. und hat eine Beziehung OneToMany mit Konto (Konto Tabelle ID ist ein Fremdschlüssel in EAS als Konto_ID). Bitte sehen Sie das Bild

Jetzt muss ich die account.id, Gesamtausgaben (die die Summe des Betrags_spend mit der gleichen account_id ist) und letzten ausgeben (ist der letzte Datensatz in Aschetabelle gegen und account_id ie Betrag_Spend-Wert mit MAX (ash.id)) entspricht, ist, dass

id | spend_total | last_spend 
--------------------------------- 
1 | 30   | 18 
2 | 280   | 120 
3 | 20   | 20
SELECT a.id, SUM(ash.amount_spend) AS spend_total 
FROM accounts as a 
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id 
GROUP BY a.id

Im die account.id und die Summe der ash.amount Ausgaben bekommen, aber ich brauche auch zuletzt verbringen. Wie bekomme ich das?

Danke.

Antwort

2

Hier ist eine Option ein correlated subquery:

SELECT a.id, SUM(ash.amount_spend) AS spend_total, 
     (SELECT amount_spend 
     FROM account_spend_history as ash2 
     WHERE ash2.account_id = a.id 
     ORDER BY ash2.id DESC LIMIT 1) as last_spend 
FROM accounts as a 
    INNER JOIN account_spend_history AS ash ON ash.account_id = a.id 
GROUP BY a.id 
+0

Dank @sgeddes, es funktioniert für ich, und ich habe die gewünschte Ausgabe. –

0

Sie können eine Verwendung in Klausel und Unterabfrage

SELECT a.id, SUM(ash.amount_spend) AS spend_total, x.last_spend 
FROM accounts as a 
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id 
INNER JOIN ( 
    select account_id, last_spend, start_date 
    from account_spend_history 
    where (start_date, acoount_id) in ( 
     select max(start_date), account_id 
     from account_spend_history 
     group by account_id 
    ) 
) x on x.id = a.id 
GROUP BY a.id 
1

Sie können die MAX(ash.id) auch in Ihrer Anfrage erhalten und es dann wieder verbinden verwenden zu account_spend_history tabelle:

SELECT id, spend_total, amount_send as last_spend 
FROM (
    SELECT a.id, SUM(ash.amount_spend) AS spend_total, MAX(ash.id) AS ash_id 
    FROM accounts as a 
    INNER JOIN account_spend_history AS ash ON ash.account_id = a.id 
    GROUP BY a.id) AS t1 
INNER JOIN account_spend_history AS ash ON ash.id = t1.ash_id 
+0

Ich mag diesen Ansatz besser als das, was ich gepostet habe ... – sgeddes

+0

Hallo @Giorgos Betsos, danke, dass du mir geholfen hast. Ich schreibe die Abfrage, aber ich habe einen Fehler bekommen. ** # 1054 -. Unbekannte Spalte 'amount_send' in 'field list' ** SELECT t1.id, spend_total, amount_send AS last_spend VON (SELECT a.id, SUM (ash.amount_spend) AS spend_total, MAX (ash.id) AS ash_id FROM Rechnung als INNER JOIN AS ash account_spend_history ON ash.account_id a.id = GROUP BY a.id ) AS t1 INNER JOIN als Asche am Ascher account_spend_history. id = t1.ash_id –