If you usually want to request specific subsets of categories from a pivot table, there may be a workaround based on the comment approach.
You can fill in your "pivot_table" from the original like this:
insert into pivot_table (id, Name, json_cats) ( select id, Name, '{' || listagg(quote_ident(Category) || ':' || count, ',') within group (order by Category) || '}' as json_cats from to_pivot group by id, Name )
And access to certain categories this way:
select id, Name, nvl(json_extract_path_text(json_cats, 'Snacks')::int, 0) Snacks, nvl(json_extract_path_text(json_cats, 'Beer')::int, 0) Beer from pivot_table
Using varchar(max) for the JSON column type will give 65535 bytes, which should be space for several thousand categories.
systemjack
source share