Home > Software engineering >  Group by applied seperately for each type of column in sql
Group by applied seperately for each type of column in sql

Time:12-14

The table I'm working on is as follows : Table in Database

What I want to do is to Group By 'Month' for each Person, that is use the Group By statement multiple times for each person. I want the output as follows :

Required Table

Please tell me how I can achieve this. Thank you.

I tried looking for some for loop syntax in mysql so I could loop through each person and apply group by each time and join but that just seemed too complicated and I though there must be an easier way.

CodePudding user response:

The following query should give you the desired results:

SELECT  PERSON,
        SUM(AMOUNT) AS AMOUNT,
        MONTH
FROM    table
GROUP BY MONTH, PERSON
ORDER BY PERSON, MONTH DESC
  • Related