Distinct values for a portion of a column list

Need to develop a script that selects values when the first two selected columns are distinct, but the remaining columns are not distinct.

Need all values when only the first two requested columns are distinct and the remaining column values are not distinct

create table tab2(serial_number number,stock_number number, weight number, class number);
insert into tab2 values(100,4,5,9);
insert into tab2 values(100,5,5,8);
insert into tab2 values(100,5,5,9);
insert into tab2 values(100,9,6,7);
insert into tab2 values(101,4,5,9);
insert into tab2 values(102,5,6,8);
insert into tab2 values(102,6,5,9);
insert into tab2 values(103,4,5,9);
insert into tab2 values(110,1,6,19);
insert into tab2 values(110,1,5,9);
insert into tab2 values(110,2,5,9);
commit;


select distinct serial_number, stock_number, weight, class
from tab2

returns values only when all four columns contain a distinct combination of values:

SERIAL_NUMBER STOCK_NUMBER     WEIGHT      CLASS
------------- ------------ ---------- ----------
          100                               9                        6                    7
          100                               5                        5                   8
          103                              4                          5                  9
          110                              1                         5                  9
          100                              4                         5                  9
          100                              5                        5                   9
          110                              2                        5                   9
          101                              4                        5                   9
          102                              5                        6                   8
          102                              6                        5                   9
         
desired results:
SERIAL_NUMBER STOCK_NUMBER     WEIGHT      CLASS
------------- ------------ ---------- ----------
        100                             5                          5                  8
        100                             5                          5                  9
        110                             1                          6                19
        110                             1                          5                  9
J RAsked:
Who is Participating?
 
johnsoneSenior Oracle DBACommented:
This gives me the results you asked for:
SELECT serial_number, 
       stock_number, 
       weight, 
       class 
FROM   tab2 
WHERE  ( serial_number, stock_number ) IN (SELECT serial_number, 
                                                  stock_number 
                                           FROM   tab2 
                                           GROUP  BY serial_number, 
                                                     stock_number 
                                           HAVING Count(1) > 1); 

Open in new window

0
 
J RAuthor Commented:
Yes it does.  Thank you  very much..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.