Does anyone know the purpose of cardinality() function in sql? How does it work with unnest function?
SELECT story_id,
cardinality(image_tags) AS image_tags,
cardinality(comment_tags) as comment_tags
from
story_tab
CROSS JOIN UNNEST(data.images) t (url, item_tags, comment_tags)
Advertisement
Answer
cardinality() — as explained in the documentation — returns the size of an array.
In this case, image_tags and comment_tags are array columns in data.images. This is counting the size of those arrays for each url.
I should note that data.images is, itself, an array, so each image has these tags.
So, this is returning the number of image tags and the number of comment tags for each image in data. I would recommend including information about the image too — or aggregating by the story.