473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

where clause messed up

Hi,

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".

Erhm...`Hope u get my meaning...

/Pip
Jul 20 '05 #1
3 1748
[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
Jul 20 '05 #2
Erland Sommarskog wrote:
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.
Yes. Thats what I want
The you filter this table to only show rows where Tvalues.nr is 1.
As NULL is not 1 those rows are moved away.
Yes...this is the essence of my "problem". However I still want those
entries in Template that hasnt got a corresponding entry in TValues....
To get the result you want there three possibilities:

1) WHERE tvalues.nr = 1 OR tvalues.nr IS NULL
True...this does actually return what I want.
2) WHERE coalesce(t.values.nr, 1) = 1
You do mean "coalesce(TValues.nr, 1)" right?
Could you explain this function in "english" :) ?
The explanation in my msdn isnt that good. Something like
Return Tvalues.nr if Tvalues.nr is NULL - otherwise return 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.


I dont get this one? You just mean "...AND TValues.nr=1"?

/Pip
Jul 20 '05 #3
PipHans (pi*****@hotmail.com) writes:
2) WHERE coalesce(t.values.nr, 1) = 1
You do mean "coalesce(TValues.nr, 1)" right?


Yes. Sorry about the typo.
Could you explain this function in "english" :) ?


coalesce() accepts a list of values as arguments, and returns the
first non-NULL value in the list, or NULL if all arguments have
the value NULL.

isnull() is an older function with a clearer name, but it only accepts
two arguments. coalesce() is ANSI standard.
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.


I dont get this one? You just mean "...AND TValues.nr=1"?


For this query, just change WHERE to AND, yes.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

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

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

Similar topics

47
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
2
by: steve | last post by:
To gain performance, do I need to index ALL the fields in the where clause. Say we have a query like: select stuff from table where field1=.. and field2=... If field1 selection substantially...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
12
by: =?ISO-8859-1?Q?Ren=E9?= | last post by:
Hi, is there a rule of thumb what is better/faster/more performant in SQL Server 2005? a) SELECT * FROM A INNER JOIN B ON B.ID = A.ID AND B.Cond1 = 1 AND B.Cond2 = 2 b) SELECT * FROM A INNER...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.