SELECT
client_id,
contract_id,
tax_period_year,
COUNT(*) AS row_count,
ARRAY_AGG(id ORDER BY id) AS item_ids,
ARRAY_AGG(registry_id ORDER BY id) AS registry_ids,
ARRAY_AGG(status ORDER BY id) AS statuses
FROM report.unv_registry_item
GROUP BY
client_id,
contract_id,
tax_period_year
HAVING COUNT(*) > 1
ORDER BY
client_id,
contract_id,
tax_period_year;