English 中文(简体)
简化和降低反就业问题的成本
原标题:Simplifying and reducing the cost of an anti-join query
  • 时间:2010-12-29 11:00:21
  •  标签:
  • oracle

请帮助我简化和降低以下询问的费用?

我试图使其成为与NOT EXISTS共同相关的分局,但它没有给我任何产出。

请注意,主问和内询问的表格与表1相同。

SELECT *
FROM Table_1 A
WHERE A.Col1              =  abcd 
AND (A.Col2, A.Col3) NOT IN
  (SELECT Col2,
    Col3
  FROM Table_1 B
  WHERE (B.Col4 IN (1,2,3)
  And B.Col5    In ( x , y ))
  OR (B.Col4     = 1 AND B.Col5     =  z  AND B.Col6     =  f )
  ))

Thanks in advance, Savitha

最佳回答

Usually its a lot about trial and error. In addition to using the not exists and outer join, since this is a self-join, it should reduce down to a single table...

SELECT *
FROM Table_1
WHERE col1 =  abcd 
AND NOT
   (col4 IN (1,2,3)
      AND col5 IN ( x , y )
)
AND NOT (col4=1
  AND col5= z 
  AND col6= f 
);

或你可以尝试使用MINUS......

SELECT *
FROM Table_1
WHERE col1 =  abcd 
MINUS
SELECT *
FROM Table_1
WHERE col1 =  abcd 
AND ((col4 IN (1,2,3)
      AND col5 IN ( x , y ))
   OR (col4=1
      AND col5= z 
      AND col6= f )  
);

最有效的解决办法将取决于数据的传播。

但它没有给我任何产出。

你们是否相信有配对网?

问题回答

Try the following to see if it helps:

SELECT A.*
  FROM (SELECT *
          FROM Table_1
           WHERE A.Col1 =  abcd ) A
LEFT OUTER JOIN (SELECT Col2, Col3, primary_key_column
                   FROM Table_1
                   WHERE (B.Col4 IN (1, 2, 3) AND
                          B.Col5 IN ( x ,  y )) OR
                         (B.Col4 = 1 AND
                          B.Col5 =  z  AND
                          B.Col6 =  f )) B
  ON (B.Col2 = A.Col2 AND B.Col3 = A.Col3)
WHERE B.primary_key_column IS NULL;

你们应该有以下指数:TABLE(1Col1)、TABLE(1Col2、Col3)和TABLE1的主要栏目,不管其可能是什么。

分享和享有。

Cool answer. Shouldn t the following be the same if indexes are used

SELECT A.*
  FROM Table_1 A
LEFT OUTER JOIN (SELECT Col2, Col3, primary_key_column
                   FROM Table_1
                   WHERE (B.Col4 IN (1, 2, 3) AND
                          B.Col5 IN ( x ,  y )) OR
                         (B.Col4 = 1 AND
                          B.Col5 =  z  AND
                          B.Col6 =  f )) B
  ON (B.Col2 = A.Col2 AND B.Col3 = A.Col3)
WHERE A.Col1 =  abcd 
  and B.primary_key_column IS NULL;




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签