Is there a way to GROUP BY using one column, but a set of values it could equal for a given group?
For instance, say I have the records:
id = 1, quantity = 40, type = 1
id = 2, quantity = 50, type = 2
id = 3, quantity = 20, type = 1
id = 4, quantity = 30, type = 3
id = 5, quantity = 60, type = 3
And I would like to group types 1 and 2 together, and group type 3s by themselves. Is there a simple way to do this in MySQL, or am I better off doing this after I get the results?
Sure, you can create a custom group using a CASE statement:
Edit:
Using the result of a CASE statement, via alias, in the GROUP BY clause isn’t universally supported across RDBMSs, but more-recent versions of MySQL (>=5) will support it.