Вы можете использовать array_agg
для группировки значений в массиве и использовать FILTER
для ограничения значений, передаваемых в COUNT(*)
:
WITH data(id, name, status) AS (
VALUES (1, 'name1', TRUE)
, (2, 'name2', TRUE)
, (3, 'name1', TRUE)
, (4, 'name4', TRUE)
, (5, 'name1', FALSE)
, (6, 'name2', FALSE)
, (8, 'name1', FALSE)
, (9, 'name1', TRUE)
, (10, 'name1', FALSE)
, (11, 'name1', FALSE)
)
SELECT array_agg(id) AS row_data -- or array_agg(data.*) if you want all columns
, COUNT(*) FILTER (WHERE status) AS total_count_true
, COUNT(*) FILTER (WHERE NOT status) AS total_count_false
FROM data
WHERE name IN ('name1', 'name2')
возвращает
+---------------------+----------------+-----------------+
|row_data |total_count_true|total_count_false|
+---------------------+----------------+-----------------+
|{1,2,3,5,6,8,9,10,11}|4 |5 |
+---------------------+----------------+-----------------+
или, используя array_agg(data.*)
:
+----------------------------------------------+----------------+-----------------+
|row_data |total_count_true|total_count_false|
+----------------------------------------------+----------------+-----------------+
|{"(1,name1,t)","(2,name2,t)",…,"(11,name1,f)"}|4 |5 |
+----------------------------------------------+----------------+-----------------+