I want to sum Unique ids in grouped result.
My data in database field:
user_ids:
{"user_id": [213, 199, 2]}
{"user_id": [213, 2]}
{"user_id": [212]}
{"user_id": [2, 213]}
{"user_id": [266, 199]}
My Query:
select
ARRAY_AGG(DISTINCT CONCAT(jsonb_array_length(user_login_conflicts.user_ids -> 'user_id') - 1)) as user_ids
from "user_login_conflicts"
left join "users" on user_login_conflicts.user_ids -> 'user_id' @> CAST(CONCAT('[', users.id, ']') as JSONB)
where user_login_conflicts.user_ids -> 'user_id' is not null
group by "users"."id"
Result is below
user_ids:
{1,2}
{1,2}
{0}
{1,2}
{1}
I dont want to json result. I want sum result. Like this.
user_ids:
3
3
0
3
1
How can I sum my jsonb array result?
CREATEstatements for both tables andINSERTstatements for the sample data for them.SUMinstead ofARRAY_AGG, is that all?CONCATcall that converts the integer to a string for no good reason.