-1

I have a MySQL query which outputs data like this:

Output Example

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

Dan_J
  • 145
  • 15

2 Answers2

0

Try using below code, trick is to convert the date to something groupable. Not sure what you meant in the part Where transactions.tenancyID = ?

select * from 
   (Select 
      staff.firstname As stafffirstname, 
      staff.surname As staffsurname, 
      transactions.total, 
      transactions.description, 
      left(Datename(month,transactions.datetime),3) + char(39) + right(Datename(year,transactions.datetime),2) as 'Period',
      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 
Group By 
  FullResults.Period,  
  transactions.description, 
  transactions.ID,
  FullResults.stafffirstname,
  FullResults.staffsurname
JustLukas
  • 13
  • 5
0

try with group_concat, it returns one line per group element

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

How to use GROUP_CONCAT in a CONCAT in MySQL

ditch
  • 92
  • 9