[posted and mailed, please reply in news]
PipHans (pi*****@hotmail.com) writes:
Consider this (light example):
SELECT [template].[id], [template].[navn], [tvalues].[id]
FROM template LEFT JOIN Tvalues ON [tvalues].[templateid]=[template].[id]
This returns ok. But I have a clause on the Tvalues like this:
WHERE [tvalues].[nr]=1;
Now I dont get what I want anymore? What I mean here is:
"return all records from Template and the corresponding values in TValues.
If no corresponding values can be found or those where TValues.nr<>1 then
return NULL".
But what you are saying is:
Construct a table of template and Tvalues. If there is no matching
rows in Tvalues for a template, put NULL in all columns for Tvalues.
The you filter this table to only show rows where Tvalues.nr is 1.
As NULL is not 1 those rows are moved away.
To get the result you want there three possibilities:
1) WHERE tvalues.nr = 1 OR tvalues.nr IS NULL
2) WHERE coalesce(t.values.nr, 1) = 1
3) Move the condition to the ON clause.
The last alternative, which what I prefer, changes the constructed
table to only include rows from Tvalues when nr = 1. With other
values in Tvalues, the Tvalues columns will be NULL.
Note: this constructed table is a logical concept and does not relate
to how the optimizer implements the query.
--
Erland Sommarskog, SQL Server MVP,
so****@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp