I have the following table:
CREATE TABLE Kittens (
k_id INTEGER PRIMARY KEY AUTOINCREMENT,
human_name text,
tags json
);
With this data
INSERT INTO Kittens (human_name, tags) VALUES ("Charles",'["fiendish", "fluffy", "quiet", "discerning", "catlike"]');
INSERT INTO Kittens (human_name, tags) VALUES ("Tails",'["fluffy", "loud","catlike"]');
INSERT INTO Kittens (human_name, tags) VALUES ("Mittens",'["fluffy","regal","catlike"]');
I'd like to aggregate the common kitten tags, and I believe this requires the json_group_array function, which I've read about here:
However, this doesn't seem to be unrolling or aggregating the tags as I would expect, just list the lists consecutively.
sqlite> select json_group_array(tags) from Kittens;
["[\"fiendish\", \"fluffy\", \"quiet\", \"discerning\", \"catlike\"]","[\"fluffy\", \"loud\",\"catlike\"]","[\"fluffy\",\"regal\",\"catlike\"]"]
Any guidance on what this query should look like? Seeking something more like:
tag_array
catlike
catlike
catlike
discerning
fiendish
fluffy
fluffy
fluffy
loud
quiet
regal
And ultimately, with an aggregation:
tag_array count(*)
fluffy 3
catlike 3
fiendish 1
discerning 1
loud 1
regal 1
quiet 1
json_group_array()
constructs an array from its elements; to extract values from an array, use json_each()
:
SELECT tag, count(*)
FROM (SELECT j.value AS tag
FROM Kittens
CROSS JOIN json_each(Kittens.tags) AS j)
GROUP BY tag;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.