[英]Postgres: How to copy elements from text array column into a json column?
[英]How to get value and record count of elements from array column in postgres
假設我在 Postgres 中有下表:
CREATE TABLE movies (
id serial primary key,
title text,
actor_ids bigint[]
)
如何獲取每個演員出演的電影數量? 我應該這樣打印:
actor_id | count
----------------
53 | 4
92 | 13
132 | 1
1221 | 2
actor_ids 列是一個數組,其中包含電影中主演的演員的 ID。
您需要首先取消嵌套數組,然后您可以應用 group by 和 count
select a.actor_id, count(*)
from movies m
cross join unnest(m.actor_ids) as a(actor_id)
group by a.actor_id
order by a.actor_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.