-1

How do I exclude certain rows? For example, I have the following table:

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
|    1 | 1    |    R |
|    1 | 2    |    D |
|    2 | 3    |    R |
|    2 | 4    |    R |
|    3 | 5    |    R |
|    4 | 6    |    D |
+------+------+------+

I need to select only:

|    2 | 3    |    R |  
|    2 | 4    |    R |  
|    3 | 5    |    R |  

My select that does not work properly:

with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual), 
tt as (select t.*,count(*) over (partition by c1) cc from t ) select * from tt where cc=1 and c3='R';

Thanks in advance!

5
  • 3
    can you post tables but not in pictures Nov 23, 2018 at 10:00
  • Are you using MySQL or Oracle?
    – jarlh
    Nov 23, 2018 at 10:01
  • Instead of me trying to guess what you are trying to do on the basis of non working code you could tell me.
    – P.Salmon
    Nov 23, 2018 at 10:12
  • I'm using PL/SQL developer v.10
    – Karina
    Nov 23, 2018 at 10:25
  • 2
    What is the logic behind why you only want to select those two rows? Is it that you're trying to select rows where the col3 values are all 'R' across each col1 value? Or is it rows where the col3 values are all the same for each col1 value? Or something else? Please edit your question to add in why you want to select those rows and not any others.
    – Boneist
    Nov 23, 2018 at 10:44

7 Answers 7

0
select * from table where col2 = 'R'

or if you want to exclude rows with D value just

select * from table where col2 != 'D'

0

It depends on your requirements but you can do in this way:

SELECT * FROM `table` WHERE col1 = 2 AND col3 = "R"

if you want to exclude just do it like WHERE col1 != 1

You ca also use IN clause also e.g.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

This syntax is for MySql, but you can modify it as per your requirement or database you are using.

0

this will work :

select * from (select * from table_name) where rownum<=4
minus 
select * from ( select * from table_name) where rownum<=2
6
  • How should that work? There is no row named rownum in the example
    – Nico Haase
    Nov 23, 2018 at 10:35
  • @NicoHaase its called pseudo column bro rownum is a pseudo column Nov 23, 2018 at 10:43
  • What kind of "pseudo column" is that? Does PostgreSQL support it?
    – Nico Haase
    Nov 23, 2018 at 10:46
  • @NicoHaase its tagged oracle thats why Nov 23, 2018 at 10:47
  • Sorry, my bad. Misremembered PL/SQL. But your answer could benefit from an explanation about these numbers. Why did you use 4 in the first and 2 in the second part?
    – Nico Haase
    Nov 23, 2018 at 10:51
0

My guess is that you want all rows for a col1 where no row for a col1 = D and at least 1 row for a col1 = R. @ where [not] exists may do

DROP TABLE T;
CREATE TABLE T
(Col1 NUMBER, Col2 NUMBER, Col3 VARCHAR(1));
INSERT INTO T VALUES (    1 , 1    ,    'R');
INSERT INTO T VALUES (       1 , 2    ,    'D'); 
INSERT INTO T VALUES (       2 , 3    ,    'R'); 
INSERT INTO T VALUES (       2 , 4    ,    'R'); 
INSERT INTO T VALUES (       3 , 5    ,    'R');
INSERT INTO T VALUES (       3 , 6    ,    'D');
INSERT INTO T VALUES (       4 , 5    ,    'X');
INSERT INTO T VALUES (       4 , 6    ,    'Y');
INSERT INTO T VALUES (       5 , 6    ,    'X');
INSERT INTO T VALUES (       5 , 5    ,    'R');
INSERT INTO T VALUES (       5 , 6    ,    'Y');

SELECT * 
FROM T
WHERE NOT EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'D') AND
      EXISTS(SELECT 1 FROM T T1 WHERE T1.COL1 = T.COL1 AND COL3 = 'R');

Result

      COL1       COL2 COL3
---------- ---------- ----
         5          6 X    
         5          5 R    
         5          6 Y    
         2          3 R    
         2          4 R   
0

use row_number() window function

    with t (c1,c2,c3) as(
select 1 , 1 , 'R' from dual union all
select 1 , 2 , 'D' from dual union all
select 2 , 3 , 'R' from dual union all
select 2 , 4 , 'R' from dual union all
select 3 , 5 , 'R' from dual union all
select 4 , 6 , 'D' from dual
    ),
  t1 as
  (
      select c1,c2,c3,row_number() over(order by c2) rn from t 
  ) select * from t1  where t1.rn>=3 and t1.rn<=5

demo link

C1  C2  C3
2   3   R
2   4   R
3   5   R
2
  • 1
    Why downvote for this answer. It looks correct to me. As per given data this query should result in the expected output.
    – XING
    Nov 23, 2018 at 10:12
  • @XING don't know why they have given downvote but thanks to you Nov 23, 2018 at 10:21
0

You can try using correlated subquery

select * from tablename a
from 
where exists (select 1 tablename b where a.col1=b.col1 having count(*)>1)
0

Based on what you have provided I can only surmise that the only requirement is for COL1 to be equal to 2 or 3 in that case all you have to do is (assuming that you actually have table);

SELECT * FROM <table_name>
WHERE col1 IN (2,3);

This will give you the desired output for the particular example provided in the question. If there is a selection requirement that goes beyond retrieving data where column 1 is either 2 or 3 than a more specific or precise answer can be provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.