Abhishek Singh
Abhishek Singh

Reputation: 1517

Filter rows from group by clause postgresql

I have below table, Now I want group by on col_1, and want to select data of that group which has col_2='ValueNet' and col_3='no' and also want to exclude row which satisfy before said condition. (i.e col_2='ValueNet' and col_3='no')

col_1  | col_2     |   col_3 <br>
1     | Community1 |  ACSNET    
1     | Community2 |  ACSNET    
1     | ValueNet   |  yes   
2     | Community3 |  ACSNET    
2     | Community4 |  ACSNET    
2     | ValueNet   |  no     

my result table should be:

col_1    | col_2    |   col_3 <br>
2     | Community3 |  ACSNET    
2     | Community4 |  ACSNET    

Upvotes: 0

Views: 234

Answers (1)

LirHaz
LirHaz

Reputation: 36

One option is to make a query that finds from the table all the results according to the condition you mention: "col_2='ValueNet' and col_3='no'", and then to make an except clause for what you want to exclude:

select * from [Table_1] where col_1 in
(select col_1 from [Table_1] 
where col_2 ='ValueNet' and col_3='no')
except
(select * from [Table_1] 
 where col_2 ='ValueNet' and col_3='no')

Upvotes: 1

Related Questions