简体   繁体   中英

Selecting data from multiple rows

  1. For example test column 7 contains two rows, if the number column contains values 5 AND 6, AND the value is NOT X in the chr column, I would like to select select the rows with 7 in the test column.

  2. For example test column 10 contains three rows, if the number column contains values 5 AND 6, AND the value X exists in the chr column, I would like to exclude rows with 10 in the test column.

The Demo of the below Schema and broken SQL query is available on SQL fiddle.

Schema:

CREATE TABLE TEST_DATA (ID INT, TEST INT, CHR VARCHAR(1), NUMBER INT);

INSERT INTO TEST_DATA VALUES 
( 1    ,    7 , 'C'   ,       5),
( 2    ,    7 , 'T'   ,       6),
( 3    ,    8 , 'C'   ,       4),
( 4    ,    8 , 'T'   ,       5),
( 5    ,    9 , 'A'   ,       4),
( 6    ,    9 , 'G'   ,       5),
( 7    ,   10 , 'T'   ,       4),
( 8    ,   10 , 'A'   ,       5),
( 9    ,   10 , 'X'   ,       6),
(10    ,   14 , 'T'   ,       4),
(11    ,   14 , 'G'   ,       5);

SQL:

SELECT *
FROM test_data t1, test_data t2
WHERE t1.number=5 is not t1.chr=X AND
      t2.number=6 is not t2.chr=X;

How would it be possible to keep test column if number columns contains 5 and 6 and the chr column does not contain X ?

UPDATE As result it should only be test column with 7, because test column 7 have 5 and 6 in the number column and not X.

UPDATE 2 Result example:

ID | TEST | CHR  |  NUMBER               
1  |  7   |  C   |    5        
2  |  7   |  T   |    6   

If I understand the requirement correctly...

SELECT a.test 
  FROM test_data a
  LEFT
  JOIN test_data b
    ON b.test = a.test 
   AND b.chr = 'x'
 WHERE a.number IN (5,6)
   AND b.id IS NULL
 GROUP
    BY a.test
HAVING COUNT(*) = 2;

http://www.sqlfiddle.com/#!2/1939f/4

You can join this result back on to test_data to get all results with a test equal to 7

Try this.

Query

SELECT *
FROM test_data
WHERE number IN (5,6) 
AND test NOT IN (10)
AND chr NOT IN ('X');

Fiddle Demo

Is this what you need?

[EDIT]

select o.*
FROM
test_data o,
(SELECT o.TEST
FROM test_data o
WHERE o.NUMBER = 5) five,
(SELECT o.TEST
FROM test_data o
WHERE o.NUMBER = 6) six
WHERE five.TEST = six.TEST
AND o.TEST = five.TEST
AND o.TEST NOT IN (SELECT distinct TEST FROM test_data WHERE chr = 'X')

The result is,

ID | TEST | CHR | NUMBER

1   7      C      5 
2   7      T      6 

You want it like this ?

Query

SELECT *
FROM test_data
WHERE ID NOT IN 
(
   SELECT ID 
   FROM test_data 
   WHERE CHR='X' 
   AND NUMBER IN (5,6)
 ) 
 AND NUMBER IN (5,6)

Result

ID | TEST | CHR  |  NUMBER               
1  |  7   |  C   |    5        
2  |  7   |  T   |    6     
4  |  8   |  T   |    5  
6  |  9   |  G   |    5  
8  |  10  |  A   |    5  
11 |  14  |  G   |    5  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM