473,467 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Filtered joins

I am having some difficulty in constructing outer joins. I have
simplified what I need to do and have included sample SQL statements:

create table tab_a (id int, descr varchar(10), qty int)
insert into tab_a values (1, 'item one', 10)
insert into tab_a values (2, 'item two', 20)
insert into tab_a values (3, 'item three', 30)
insert into tab_a values (4, 'item four', 40)

create table tab_b (id2 int, descr2 varchar(10), qty2 int)
insert into tab_b values (1, 'item one', 10)
insert into tab_b values (2, 'item two', 20)
insert into tab_b values (3, 'item three', 30)
insert into tab_b values (4, 'item four', 40)

Here is the statement that I have:

SELECT tab_a.id,
tab_a.descr,
tab_a.qty,
tab_b.id2,
tab_b.descr2,
tab_b.qty2
FROM tab_a LEFT OUTER JOIN tab_b
ON (tab_a.id = tab_b.id2 )
WHERE tab_a.qty <= 30 AND
tab_b.qty2 > 20

What I am trying to do is left outer join between tab_a and tab_b after
they have been filtered based on the qty column. (for tab_a: qty <=
30; and for tab_b: qty > 20).

How would I go about that? I would like to do this efficiently since
the two tables have about a million records and several other columns
each.

Jul 23 '05 #1
5 1160
On 20 Jan 2005 06:40:53 -0800, ne**********@yahoo.com wrote:

(snip)
How would I go about that? I would like to do this efficiently since
the two tables have about a million records and several other columns
each.


Hi newtophp,

You forgot to include the desired output from your query. Based on your
narrative, I *think* that this query might do what you want:

SELECT tab_a.id, tab_a.descr, tab_a.qty,
tab_b.id2, tab_b.descr2, tab_b.qty2
FROM tab_a
LEFT OUTER JOIN tab_b
ON tab_a.id = tab_b.id2 AND tab_b.qty2 > 20
WHERE tab_a.qty <= 30
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Hugo Kornelis wrote:
On 20 Jan 2005 06:40:53 -0800, ne**********@yahoo.com wrote: Hi newtophp,

You forgot to include the desired output from your query. Based on your narrative, I *think* that this query might do what you want:

SELECT tab_a.id, tab_a.descr, tab_a.qty,
tab_b.id2, tab_b.descr2, tab_b.qty2
FROM tab_a
LEFT OUTER JOIN tab_b
ON tab_a.id = tab_b.id2 AND tab_b.qty2 > 20
WHERE tab_a.qty <= 30
Thanks Hugo! It works as expected now. Essentially, what I was
looking for was standard outer join output:

id descr qt id2 descr2 qty2
==================================
1 item one 10 NULL NULL NULL
2 item two 20 NULL NULL NULL
3 item three 30 3 item three 30

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #3
> Hugo Kornelis wrote:
Based on your
narrative, I *think* that this query might do what you want:

SELECT tab_a.id, tab_a.descr, tab_a.qty,
tab_b.id2, tab_b.descr2, tab_b.qty2
FROM tab_a
LEFT OUTER JOIN tab_b
ON tab_a.id = tab_b.id2 AND tab_b.qty2 > 20
WHERE tab_a.qty <= 30
Thanks Hugo! It works as expected now.

Hi Hugo,

After playing with the above statement, I noticed that this form also
works: (I put extra blank lines to highlight the difference):

SELECT tab_a.id, tab_a.descr, tab_a.qty,
tab_b.id2, tab_b.descr2, tab_b.qty2
FROM tab_a
LEFT OUTER JOIN tab_b
ON tab_a.id = tab_b.id2 AND tab_b.qty2 > 20

AND tab_a.qty <= 30

WHERE tab_a.qty <= 30

The difference is that the filter "tab_a.qty <= 30" is repeated in both
the ON clause and the WHERE clause. Would this form make the query
execute faster or slower? It appears that it prunes out rows even
before the outer join so it would be more efficient but I am not
certain.

What is your take on this?

Thanks!


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)


Jul 23 '05 #4
On 20 Jan 2005 18:12:41 -0800, ne**********@yahoo.com wrote:
After playing with the above statement, I noticed that this form also
works (snip) Would this form make the query
execute faster or slower? It appears that it prunes out rows even
before the outer join so it would be more efficient but I am not
certain.

What is your take on this?


Hi newtophp2000,

The only way to find out is to test is and compare execution plans.

I ran a test with the tables and sample data you posted. With these
tables, this sample data and my hardware configuration, the plans were
very similar. Both execution plans start with a table scan on table a,
already filtering for the requirement on tab_a.qty. Both queries then join
table b (using a table scan on table b for each row returned from the scan
on table a, filtering for the value of tab_b.qty2). The only difference I
found is that your query would compare the value of tab_a.qty to 30 a
second time during the join operation.

On your system, with your real tables, real data and (hopefully) some
useable indexes, you're very likely to get different results. So you
really should test it yourself.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #5
(ne**********@yahoo.com) writes:
After playing with the above statement, I noticed that this form also
works: (I put extra blank lines to highlight the difference):

SELECT tab_a.id, tab_a.descr, tab_a.qty,
tab_b.id2, tab_b.descr2, tab_b.qty2
FROM tab_a
LEFT OUTER JOIN tab_b
ON tab_a.id = tab_b.id2 AND tab_b.qty2 > 20

AND tab_a.qty <= 30

WHERE tab_a.qty <= 30

The difference is that the filter "tab_a.qty <= 30" is repeated in both
the ON clause and the WHERE clause. Would this form make the query
execute faster or slower? It appears that it prunes out rows even
before the outer join so it would be more efficient but I am not
certain.


The optimizer may be smart enough to only check the condition once.

Generally, maybe we should explain why you had to rewrite the query.

If you say:

SELECT ...
FROM a LEFT JOIN b ON a.col = b.col

You get a table which has many rows as there are rows in a, and
which includes all columns in a and b. For the rows in a where there are
matching rows in b, there are values in the b columns, for other
rows all columns in b are NULL.

Then we apply a filter:

SELECT ...
FROM a LEFT JOIN b ON a.col = b.col
WHERE b.othercol = 2

The WHERE clause filters the first table, so that only rows with
b.othercol = 2 remains. This means that all rows with NULL in b.othercol
are lost. Specifically that means that all rows in a without matching
rows in b are lost.

If we move the condition to the ON clause, the result is different:

SELECT ...
FROM a LEFT JOIN b ON a.col = b.col
AND b.othercol = 2

Now there will be NULL in all b-columns for all rows in a that does
not match a row in b where othercol = 2.

More generally, the FROM JOIN clauses are evalutated in order to build
table by table, and then the final table is filtered by WHERE.

CONCEPTUALLY THAT IS! The actual computation order performed by
SQL Server may be totally different. The optimizer is free to recast
as long as the result does not change.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
7
by: Ken | last post by:
Hi All - I have a filtered GridView. This GridView has a check box in the first column. This check box is used to identify specific rows for delete operations. On the button click event I...
1
by: Ken | last post by:
I have a form that has a command button on it to open a report. The report is based on the forms data, if it's filtered the report is filtered, if the form is showing 100 records the report is...
8
by: Radu | last post by:
Hi, I have the following problem: I open a recordset based on excel/csv files, but then I need to filter (in code) in order to extract only data pertaining to a specific person. This code is...
7
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
3
by: melnhed | last post by:
---Report the current filtered records from a Form--- Hello All, I've seen this topic discussed before, but the solution described then doesn't work in my particular case. My Config: ...
0
by: Ironr4ge | last post by:
Hi everyone, By the rate its going it want be long till I start growing gray hair... but anyway.. to come to the point... I am trying to open the form "Languages" with a diffrent record...
4
by: Ironr4ge | last post by:
Hi everyone, I am trying to open the form "Languages" with a diffrent record source to the "Contacts" form where I conducted the search or filter... . I was wondering whether there was a vba...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.