Id parent id 1. Null 2. Null 3. 1 4. 1 5. 1 6. 2 7. 2 8. 2 9. 2 10. 2 I have this table
I want something like this
I have one parameter
Min parent id count Which means if i set min parent id count 2 I should get row with id 1 and raw with id 2 Cause row with id 1 is used in parent id for 3 times And row with id 2 is used in parent id for 5 times If set min parent id count to 4 I should only get row which has id 2 cause id is used in parent id for 5 times What i want is to check for every row how many times its id value used in as value in parent id For here is 1 is used for 3 times And id 2 is used for 5 times 21 Answer
To get the number of ids in column parent_id :
SELECT parent_id, count(*) FROM my_table GROUP BY parent_id To get rows whose id corresponds to parent_id with number >= 4 :
WITH list AS ( SELECT parent_id FROM my_table GROUP BY parent_id HAVING COUNT(*) >= 4 ) SELECT t.* FROM my_table AS t INNER JOIN list AS l ON l.parent_id = t.id 2