I have a MySQL query which outputs data like this:
This is my current query that I'm using to generate the output.
SELECT *
FROM (SELECT staff.firstname AS stafffirstname
, staff.surname AS staffsurname
, transactions.total
, transactions.description
, transactions.datetime
, transactions.ID
, (@runningtotal := @runningtotal + transactions.total) AS balance
FROM transactions
LEFT JOIN staff ON transactions.createdbyID = staff.ID
WHERE transactions.tenancyID = ?
ORDER BY transactions.datetime
, transactions.ID) FullResults
ORDER BY FullResults.datetime
, FullResults.ID
LIMIT ? , ?;
So it's doing a full query to build the running balance, and then selects a subset to output. The table I'm using supports paging.
How can I take the data and group all the subitems together by month? IE, all the November Standing Charges should only have one line in the table. And all the November Usage Charges should only have one line.
UPDATE
This is my query so far with help from JustLukas.
SELECT
FullResults.*
FROM
(SELECT
staff.firstname AS stafffirstname,
staff.surname AS staffsurname,
transactions.total,
transactions.description,
transactions.datetime,
transactions.ID,
(@runningtotal := @runningtotal + transactions.total) AS balance,
Concat(CONVERT(IfNull(transactions.meterID, ''), CHAR), ";", CONVERT(transactions.typeID, CHAR), ";",
Left(MonthName(transactions.datetime), 3), CONVERT(Year(transactions.datetime), CHAR)) AS GroupingField
FROM
transactions
LEFT JOIN staff ON transactions.createdbyID = staff.ID
WHERE
transactions.tenancyID = 2631
ORDER BY
transactions.datetime,
transactions.ID) FullResults
GROUP BY
FullResults.GroupingField
How do I get the SUM(transaction.total) for each group? At the moment, it's only showing me the total for the first row in each group.
Thanks Dan