On 12 Aug 2004 03:47:15 -0700, kieran wrote:
Hi Guys,
I think I have an example of where it happens now. If you paste all
the first statement into Query Analyser and run it. Then paste the
second select statement into query analyser you will see two rows
returned.
I think this is the easiest way to see what I am talking about. RE:
where it is used - the tblStaff is a large table with many fields (i
detailed the basic for claity) where all staff details are pulled
from. There is many users on it and I was worried when I saw this why
it was happening. And at this stage im also very curious why this is
happening. Hope you can see what I mean as i know im not going crazy.
Cheers.
(snip DDL, sample data and query - thanks for providing it!)
Yes, this will indeed return two rows. They are two DIFFERENT rows,
though, not two copies of the same row. If you want to see more clearly
what's happening, change your query to read:
SELECT T2.FirstName AS StandIn_FirstName, T2.LastName AS
StandIn_LastName, T2.StaffNo
FROM tblStaff AS T1
LEFT OUTER JOIN tblStaff AS T2
ON T1.StaffNo = T2.StandIn
WHERE (T1.StaffNo = 2)
(That is: add T2.StaffNo to the select-list).
You'll see that the two rows returned are for staffno 1 and 2.
First, let's find out what exactly the contents of the table is after the
updates but before the select (leaving out the names - they are the same
on each row and won't influence the results)
SELECT StaffNo, StandIn
FROM tblStaff
StaffNo StandIn
----------- -----------
1 2
2 2
3 1
Here's what the query does (logically speaking - the exact order of
evaluation chosen by SQL Server may differ as long as the results remain
the same).
First, two copies of tblStaff are made in a working area; they are joined
so that a row from T1 will be combined with a row from T2 if the person in
T1 can be a standin for T2. If a row from T1 has no matching row in T2, it
is combined with a bunch of NULL values (as a result of the LEFT OUTER
JOIN). The intermediate results will be (agian leaving out the names):
<----- T1 -----> <----- T2 ----->
StaffNo StandIn StaffNo StandIn
----------- ----------- ----------- -----------
1 2 3 1
2 2 1 2
2 2 2 2
3 1 NULL NULL
The first row denotes that StaffNo 1 is standin for StaffNo 3. The second
and third row denote that StaffNo 2 is standin for StaffNo 2 (him/herself)
and 1. The final row denotes that StaffNo 3 is standin for nobody.
The WHERE clause filters the intermediate results above. Only the rows
with T1.StaffNo = 2 are retained. These are the two rows denoting that
StaffNo 2 is standin for StaffNo 1 and 2.
Finally, the SELECT clause defines what should be returned. In the case of
your original query, this will return the first and last name of the two
employees that have employee 2 as a standin.
While the above (hopefully) clarifies why you get two rows, it doesn't
solve your problem. In order to do that, I really must now what output you
would expect and why you expect that output. Once you post that, I (and
the other regular posters in this group) can try to find you a better
query.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)