I have a community driven database for an iOS game. I am trying to aggregate the data I’ve collected by the community, and it looks something like this:
+--------+--------+--------------+
| color1 | color2 | result_color |
+--------+--------+--------------+
| red | blue | purple |
| blue | red | purple |
| red | blue | purple |
| yellow | blue | green |
+--------+--------+--------------+
Currently, I am running the following query:
select
count(*) as count,
`Mixes`.*
from
`mixes` AS `Mixes`
where
`result_color` = 'purple'
group_by
color1,
color2
order by
`count` desc
Which produces the following output:
+-------+--------+--------+--------------+
| count | color1 | color2 | result_color |
+-------+--------+--------+--------------+
| 2 | red | blue | purple |
| 1 | blue | red | purple |
+-------+--------+--------+--------------+
However, I would like it to produce the following output, since when mixing colors it doesn’t matter which color you mix first:
+-------+--------+--------+--------------+
| count | color1 | color2 | result_color |
+-------+--------+--------+--------------+
| 3 | red | blue | purple |
+-------+--------+--------+--------------+
So, my question is, how can I aggregate data over 2 columns such that when color1 is red and color2 is blue, that the aggregate function treats it the same as when color1 is blue and color2 is red?
Thanks in advance!
If you just have the two color columns you can simply order them with a CASE expression: