views:

82

answers:

6

I want a single query to produce the following results..

To result in records that are in table1 and in table2 and were not in table3.

There are more than 10,000 records in each table..so i am looking for an efficient one. In all the table Cono is the primary key..

In detail with tables.

TABLE 1:-

Cono     

th-123
th-124
th-125

TABLE 2:-

Cono     

th-234
th-245
th-256

TABLE 3:-

Cono     

th-124
th-125
th-256

Now i want to have the following records

Result TABLE:-

Cono     

th-123
th-234
th-245
+3  A: 

Try this

WITH Table1 AS
(
    SELECT 'th-123' CONO UNION
    SELECT 'th-124' UNION
    SELECT 'th-125'
)
,
Table2 AS
(
    SELECT 'th-234' CONO UNION
    SELECT 'th-245' UNION
    SELECT 'th-256'
)
,
Table3 AS
(
    SELECT 'th-124' CONO UNION
    SELECT 'th-125' UNION
    SELECT 'th-256'
)

SELECT CONO
FROM Table1 
WHERE NOT EXISTS
(
    SELECT 1
    FROM Table3
    WHERE TABLE1.CONO = TABLE3.CONO
)

UNION ALL

SELECT CONO
FROM Table2
WHERE NOT EXISTS
(
    SELECT 1
    FROM Table3
    WHERE TABLE2.CONO = TABLE3.CONO
)
Raj More
We can use UNION ALL in this case
Vash
@Vash Tx. Answer edited.
Raj More
@Vash, on what did you base the assumption `We can use UNION ALL in this case`? for the OP's simple example data, `UNION ALL` will work but will it be the correct way in production? who is to know. I would prefer to use it as it is faster, but I'm not going to make that assumption unless the OP offers more info.
KM
@Raj - As i have mentioned there are more than 10,000 records in each tables. For example i have written only 3. Then do i need to mention, with table1 as for all the records?
satya
That was only a tip that we can (based on the presented data example), not he had to use it. You have totally right with that assumption that in those two table may be some doubled entries on the production database.
Vash
+1  A: 

try this:

select t.cono from Table1 t WHERE NOT EXISTS (SELECT 1 
                                              FROM Table3 x WHERE x.cono=t.cono)
UNION
select t.cono from Table2 t WHERE NOT EXISTS (SELECT 1 
                                              FROM Table3 x WHERE x.cono=t.cono)
KM
A: 

Try this (not tested):

; WITH all_data AS (
   SELECT * FROM table1
   UNION ALL
   SELECT * FROM table2
)
SELECT * 
FROM all_data ad
WHERE NOT EXISTS (
   SELECT *
   FROM table3 t3
   WHERE ad.Cono = t3.Cono);

Greets Flo

Florian Reischl
A: 

Kind of vague tables and names, but here's what you can do if you REALLY wanna do it in one query:

SELECT Cono
FROM Table3
WHERE NOT EXISTS ( SELECT Cono
                   FROM TABLE1 as T
                   WHERE EXISTS ( SELECT *
                                  FROM TABLE2
                                  WHERE T.Cono = TABLE2.Cono));

This should select the values in table 3 that do not exist in the query that was created in parenthesis, which is a table made up of rows that are in tables 1 and 2.

Unfortunately, nesting and efficiency normally don't go hand-in-hand...

rownage
+1  A: 
(SELECT t1.Cono FROM table1 t1
LEFT JOIN table3 t3
ON t3.Cono = t1.Cono
WHERE t3.Cono IS NULL)
UNION ALL
(SELECT t2.Cono FROM table2 t2
LEFT JOIN table3 t3
ON t3.Cono = t2.Cono
WHERE t3.Cono IS NULL)
Marcus Adams
`exists` will be faster than a `left join`, also the `UNION ALL` might create duplicates, the OP's data has not been explained enough to assume that table1.cono and table2.cono values do not overlap.
KM
@KM, please provide reference that explains that exists is faster.
Marcus Adams
see this article: http://explainextended.com/2009/06/16/in-vs-join-vs-exists/ it basically says that a JOIN and an EXISTS will sometimes have the same execution plan, and sometimes not. however when they don't the EXISTS will be slightly faster.
KM
A: 

this one worked for me... and process fast:

select X.FID, c.id as CID 
from
(
    select a.id as FID from tbl1 a
    union 
    select b.id as FID from tbl2 b
) as X
 left outer join tbl3 c on FID = c.id
where
    c.id is null
;
KoolKabin