After grouping below table by id I want to filter rows based on condition
Input table :
ID | Options |
---|---|
1 | 132 |
1 | 156 |
1 | 134 |
2 | 145 |
2 | 132 |
3 | 123 |
3 | 132 |
3 | 135 |
3 | 188 |
if a group contains 134 and 132 then only the row with 134 should be filtered. whereas the group contains only 132 then that has to be selected. Rows with options other than below table remain unchanged.
Below are the conditions
Combination of option in a group | Row with below option value in a group has to be retained |
---|---|
132,134 | 134 |
132 | 132 |
132,135 | 135 |
132,136 | 136 |
Output table :
ID | Options |
---|---|
1 | 134 |
2 | 145 |
2 | 132 |
3 | 123 |
3 | 135 |
3 | 188 |
If I understand correctly, you are saying that "134" should be the only element when "132" is also in the group.
select t.*
from (select t.*,
sum( case when options = 132 then 1 else 0 end ) over (partition by id) as num_132,
sum( case when options = 134 then 1 else 0 end ) over (partition by id) as num_134
from t
) t
where num_132 = 0 or num_134 = 0 or options = 134