Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 OUTER JOIN

Author  Topic 

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-20 : 02:56:02
How to SELECT only rows where result is different from NULL when I performed OUTER JOIN (LEFT or RIGHT)?

Nazim
A custom title

1408 Posts

Posted - 2002-02-20 : 03:02:43
Your Question is too vague. which results are you talking about (i take it as some field value, though am sure you are looking for something else).

select a.coulmn1,b.column2
from a
left outer join b
on a.commonkey=b.commonkey
where a.somefield is not null


if this isnt what you want ,post your actual question . with some sample data and the results you expect

HTH

--------------------------------------------------------------
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-20 : 03:07:33
Yes, I mean exactly that but is there any way to perform this query without WHERE clause?

quote:

Your Question is too vague. which results are you talking about (i take it as some field value, though am sure you are looking for something else).

select a.coulmn1,b.column2
from a
left outer join b
on a.commonkey=b.commonkey
where a.somefield is not null


if this isnt what you want ,post your actual question . with some sample data and the results you expect

HTH

--------------------------------------------------------------


Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2002-02-20 : 03:11:35
How about removing where clause and adding a And on join condition


select a.coulmn1,b.column2
from a
left outer join b
on a.commonkey=b.commonkey
and a.somefield is not null


HTH

--------------------------------------------------------------
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-20 : 03:15:06
Thanks HTH,

That's a good idea but it is the same I just wonder if there any type of OUTER JOIN to perform this type of query as SQL Server to do this automatically without such clause as WHERE or similar type of syntaxsis?
Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2002-02-20 : 03:25:51
Check this link
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=12983
HTH

--------------------------------------------------------------
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-02-20 : 04:01:35
Why do you ask this question?
What are you trying to do?


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-21 : 02:37:26
I am trying to get all data from TABLE A which is not in TABLE B.

quote:

Why do you ask this question?
What are you trying to do?


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.



Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2002-02-21 : 02:47:28
thought this query should help you instead of joins in doing what you want


select * from TableA
where somekey not in ( select somekey from tableb)



--------------------------------------------------------------
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-21 : 08:26:36
I know but this query is too slow and the performance is very low:( I am trying to find the fastest solution. I am dealing with a databases with a more than 200 millions of records:)

quote:

thought this query should help you instead of joins in doing what you want


select * from TableA
where somekey not in ( select somekey from tableb)



--------------------------------------------------------------


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-02-21 : 08:31:15
The OUTER JOIN with the WHERE clause will most likely be the fastest performing solution, if the columns in both tables are indexed. 200 million rows is a lot to process no matter what solution you use, you can't expect it to run in 60 seconds. How slow is it exactly anyway?

Edited by - robvolk on 02/21/2002 08:31:59
Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-21 : 08:36:11
It depends on the level of difference between both tables. The time varies between 30 min. to 6-7 hours. Do you have any idea for better speed?

quote:

The OUTER JOIN with the WHERE clause will most likely be the fastest performing solution, if the columns in both tables are indexed. 200 million rows is a lot to process no matter what solution you use, you can't expect it to run in 60 seconds. How slow is it exactly anyway?

Edited by - robvolk on 02/21/2002 08:31:59



Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-02-21 : 08:51:32
Add indexes to both tables, on the columns that they JOIN on. If you already have indexes on these columns, perform an UPDATE STATISTICS on these tables/indexes. You may also want to try a DBCC DBREINDEX on these tables, your performance problems could be from data fragmentation from a lot of INSERT/DELETE activity, and DBCC DBREINDEX will help.

These could take a lot of time to run (several hours) so you should only do it during a maintenance window; after your backups are performed, for example.

Go to Top of Page

vladimir_grigoro
Yak Posting Veteran

62 Posts

Posted - 2002-02-21 : 08:55:41
The columns are already indexed with UNIQUE, CLUSTERED, FILL FACTOR=90, WITH PAD INDEX is it OK?

Thanks for the advise I'll try.

quote:

Add indexes to both tables, on the columns that they JOIN on. If you already have indexes on these columns, perform an UPDATE STATISTICS on these tables/indexes. You may also want to try a DBCC DBREINDEX on these tables, your performance problems could be from data fragmentation from a lot of INSERT/DELETE activity, and DBCC DBREINDEX will help.

These could take a lot of time to run (several hours) so you should only do it during a maintenance window; after your backups are performed, for example.





Go to Top of Page

Lou
Yak Posting Veteran

59 Posts

Posted - 2002-02-21 : 16:32:30
[quote]
I am trying to get all data from TABLE A which is not in TABLE B.
[quote]

select a.*
from a
left outer join b
on a.commonkey=b.commonkey
where b.commonKey is null

Go to Top of Page
   

- Advertisement -