I have an sqlite table “log” that looks like this:
ID p_id viewer
----------------------
1 1 100
2 1 200
3 1 300
4 3 550
5 3 230
6 5 420
7 2 320
8 2 203
9 9 10
10 9 55
And I want to get the average viewers from each p_id. That’d be
SELECT avg(viewer) FROM log GROUP BY p_id
But I want to treat p_id 1 and 5 as the same, so I’d get the average viewers of p_id 1 and 5 combined. How do I do that? Note that the table is much larger, and I need to treat two p_id’s as the same multiple times. Can I still do a “group by” to achieve this, or are there other ways?
I tried this and it works with mySQL at least.
Edit: When using an alias-table named ‘aliases’ with the fields ‘alias_from_id’ and ‘aliased_as’:
I’ve tested this with mySQL and it works like a charm. It might be possible to simplify this SQL-query a bit but this is the best I can do at the moment 🙂
Edit2: Changed ISNULL to the corresponding SQLite
IFNULLfunctionI don’t know if SQLite supports the AS keyword, if it doesn’t then just remove that keyword – the functionality should be the same.