Home > database >  Select columns which have more the 2 values in other column
Select columns which have more the 2 values in other column

Time:02-11

I have a table which have 3 columns and we need to select data of those rows which have multiple entry in the table.

 Column1  Colum2    Colum3
 ---------------------------
 ARRG   | MEM      | 10.12
 ARRG   | MHH      | 0.245
 ARRG   | MHH      | 0.246
 ABC    | HQ       | 0.246
 CDE    | PRT      | 97.0
 TST    | TRY      | 97.0

Now I want to write a query to select rows from Column1 which have unique values in Colum2 Basically i want to print the rows of ARRG as this have 2 different values in Colum2.

ARRG   | MEM      | 10.12
ARRG   | MHH      | 0.245

CodePudding user response:

Try:

select Column1,Colum2,Colum3
from test 
where Column1 in (select Column1
                  from test
                  group by Column1
                  having count(distinct Colum2)>1
                 )
group by Column1,Colum2,Colum3;

Demo

  • Related