Postgres-当类型列具有逗号分隔值“ Action,Comedy,.... Thriller ...”时,获取每种类型的计数

时间:2019-11-20 12:46:52

标签: sql postgresql postgresql-9.3

我有下表,第四列包含每部电影的流派。我要实现的是找到每种流派的数量。

"C6523" 1894    "f" "Documentary,Short"
"C4532" 1892    "f" "Animation,Short"
"P1616" 1892    "f" "Animation,Comedy,Romance"
"B512"  1892    "f" "Animation,Short"
"B4253" 1893    "f" "Comedy,Short"
"C5215" 1894    "f" "Short"
"C6135" 1894    "f" "Short,Sport"
"E3252" 1894    "f" "Documentary,Short"

预期输出:

Animation : 12300..
Comedy: 5000...

1 个答案:

答案 0 :(得分:0)

尝试使用Sub Query

SELECT "MovieGenres", COUNT("MovieGenres")
FROM (
       SELECT UNNEST(string_to_array(subject, ',')) as "MovieGenres" FROM Table
     ) X