Question

I would like to to get the sum(gross) per month per each user AND show each event field. So I would need to show these columns

My table

  order id, user, month, event, gross
       1 jack october event2 30
       2 jack november event3 20
       3 jack november event3 20
       4 jack november event2 30
       5 sam november event2 30
       6 john october event3 20

For Jack the results would be

   record1
    user = jack
    month = november
    event = event2, event3
    total = 70

    record2
    user = jack
    month = october
    event = event3
    total = 30

So the solution I believe would be concatenating multiple event fields in one record. I have seen UNION do something like this creating comma separated values in one column but not sure how to use it here.

Was it helpful?

Solution

I think you're looking for GROUP_CONCAT()

Something like this would work:

SELECT user, month, GROUP_CONCAT(event), SUM(gross)
FROM xyz
GROUP BY month
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top