I want to sort a table with array. The record with the most overlaps should be on top. I already have a where statement to filter the records with arrays. With the same array I want to determine the number of overlaps for sorting. Do you have an idea how the order by statement might look like ?
My Table
SELECT * FROM "nodes"
+-----------+---------------------------+
| name | tags |
+-----------+---------------------------+
| Max | ["foo", "orange", "app"] |
| Peter | ["foo", "bar", "baz"] |
| Maria | ["foo", "bar"] |
| John | ["apple"] |
+-----------+---------------------------+
Result with where
SELECT * FROM "nodes" WHERE (tags && '{"foo", "bar", "baz"}')
+-----------+---------------------------+
| name | tags |
+-----------+---------------------------+
| Max | ["foo", "orange", "app"] |
| Peter | ["foo", "bar", "baz"] |
| Maria | ["foo", "bar"] |
+-----------+---------------------------+
Result with Order
SELECT * FROM "nodes" WHERE (tags && '{"foo", "bar", "baz"}') ORDER BY ????
+-----------+---------------------------+
| name | tags |
+-----------+---------------------------+
| Peter | ["foo", "bar", "baz"] |
| Maria | ["foo", "bar"] |
| Max | ["foo", "orange", "app"] |
+-----------+---------------------------+