I'm using PostgreSQL 12.2
I have a table of songs that contain traits for filtering. The traits are stored in a jsonb column like..
INSERT INTO songs(title, traitsObj)
VALUES
('song1', '{ "type1":["trait3","trait7","trait2"], "type2":[<...moreTraits>] }'),
('song2', '{ "type1":["trait3","trait9","trait6"], "type2":[<...moreTraits>] }'),
('song3', '{ "type1":["trait4","trait3","trait5"], "type2":[<...moreTraits>] }')
;
I need a query to return the count of how many times a unique trait exists in the array for each 'type' The result would be something like...
trait | times_referenced
-------------------------------
trait6 | 2355
trait3 | 1493
trait1 | 872
trait5 | 724
...
I had a solution with union before restructuring the db but now have to update this query too. I'm hung up on un-nesting the array values into a temporary table to query but I was thinking something like...
WITH
tTraits AS (
SELECT <all values for 'trait1'> AS trait, title FROM songs
)
SELECT trait, COUNT(*)
FROM tTraits
GROUP BY trait
ORDER BY times_referenced DESC;
Wouldn't be surprised if there's a cleaner way to do this without an intermediate table or WITH statement. Thank you!