I have the following selection:
SELECT
posm.id AS org_scope_metric_id,
cc.code AS control_code,
ccp.code AS part_code,
pop.id AS program_id
FROM public.org_scope_metric posm
JOIN public.org_metric pom ON pom.id = posm.metric_id
JOIN compliance.metric cm ON cm.id = pom.metric_id
JOIN compliance.control_part_metric ccpm ON ccpm.metric_id = cm.id
JOIN compliance.control_part ccp ON ccp.id = ccpm.part_id
JOIN compliance.control cc ON cc.id = ccp.control_id
JOIN compliance.program_framework cpf ON cpf.framework_id = cc.framework_id
JOIN public.org_program pop ON pop.program_id = cpf.program_id
WHERE posm.scope_id ='e6d11bfe-e5fc-11ee-8cda-238cdb93d281'
GROUP BY posm.id, cc.code, ccp.code, pop.id
If there are org_scope_metric_ids with different program_id I need to skip them all from the selection.
Please help to resolve it!
3
Answers
Thank you so much guys. My final solution according to your suggestions is
So the main thing that I had to use
OVER (PARTITION BY)
to get necessary count.Count the number of equal
org_scope_metric_id
occurrences and the number of distinctprogram_id
-s in each group and then filter result rows with both numbers greater than 1.You would need a helper function for that –
You can use
min
andmax
window functions