If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > help on a join query

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-09, 11:42
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
help on a join query

Hi, I have 2 tables table1 & table2::

TABLE1:
________________
INDEX....VALUE

1............Apple
2............Banana
3............Peaches
4............Orange
----------------

TABLE2:
________________
INDEX....COLOR

1...........Red
2...........Yellow
----------------

Now, I want to give the user a view of fruits with their respective colors. For this I want to give the user 3 options based on color:
1)show fruits with RED color
2)show fruits with Yellow color
3)show fruits with any color

I tried writing the following query:

select A.VALUE,B.COLOR
from TABLE1 as A
LEFT JOIN
TABLE2 as B
ON A.INDEX=B.INDEX
WHERE B.COLOR LIKE <variable>

my <variable> is assigned the value 1)'Red' for the first scenario,'yellow' for the 2nd & '%' for the 3rd. But with '%' I only get the following output:

________________
VALUE.....COLOR

Apple.......Red
Banana....Yellow
-----------------

While this is understandable why I am getting this output, can anybody help me in correcting the query to get the following o/p for the 3rd scenario:

_________________
VALUE.....COLOR

Apple..... Red
Banana....Yellow
Peaches
Orange
-----------------

would really appreciate any help,
thanks
Reply With Quote
  #2 (permalink)  
Old 08-20-09, 12:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
change the keyword WHERE to the keyword AND

the condition on color belongs in the ON clause of the join, not the WHERE clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-20-09, 12:08
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
NULL values cannot be compared to strings, so when you test to see if NULL is LIKE '%' the answer is NULL!
Code:
SELECT a.value
     , b.value As colour
FROM   @table1 As a
 LEFT
  JOIN @table2 As b
    ON b.id = a.id
WHERE  Coalesce(b.value, 'NO COLOUR MATCH HERE!') LIKE @variable
__________________
George
Twitter | Blog
Reply With Quote
  #4 (permalink)  
Old 08-20-09, 12:09
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
Quote:
Originally Posted by r937
the condition on color belongs in the ON clause of the join, not the WHERE clause
Are you sure about that one Rudy?

If I search for "RED" I do not want the NULLs returned.
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 08-20-09, 14:53
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
Quote:
Originally Posted by gvee
NULL values cannot be compared to strings, so when you test to see if NULL is LIKE '%' the answer is NULL!
Code:
SELECT a.value
     , b.value As colour
FROM   @table1 As a
 LEFT
  JOIN @table2 As b
    ON b.id = a.id
WHERE  Coalesce(b.value, 'NO COLOUR MATCH HERE!') LIKE @variable
thanks a ton george, that solved it. Another query by the way regarding the Coalesce command, I understand that its a kind of compressed IF ELSE construct. The syntax for Coalesce is everywhere mentioned as :

Coalesce(expr1,expr2,expr3..n) & is mentioned that it can take any number of arguments. But its failing for a single argument. Are 2 arguments the minimum??

thanks.
Reply With Quote
  #6 (permalink)  
Old 08-20-09, 15:08
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,002
Failing?

Coalesce can take oodles of parameters and returns the first non-null value
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 08-20-09, 15:21
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
I removed the second parameter from Coalesce(...) and tried...

Quote:
SQL0440N No authorized routine named "COALESCE" of type "FUNCTION" having
compatible arguments was found. SQLSTATE=42884

SQL0440N No authorized routine named "COALESCE" of type "FUNCTION
Is it confusing it with a user defined function??
I am using DB2 by the way
Reply With Quote
  #8 (permalink)  
Old 08-20-09, 15:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
Quote:
Originally Posted by rocker86
I am using DB2 by the way
maybe DB2 doesn't support all ANSI SQL functions?

you did post in the ANSI SQL forum, you know
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-20-09, 17:02
rocker86 rocker86 is offline
Registered User
 
Join Date: Jul 2009
Posts: 37
Quote:
Originally Posted by r937
maybe DB2 doesn't support all ANSI SQL functions?

you did post in the ANSI SQL forum, you know
Well I thought my query was a pretty generic SQL syntactical one so posted it here.
Nevertheless , maybe DB2 isnt allowing the single argument Coalesce function (works fine for multiple arguments in DB2).
Reply With Quote
  #10 (permalink)  
Old 08-20-09, 17:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
of what use would a single argument COALESCE be?

if foo is not NULL, then COALESCE(foo) will return foo

if foo is NULL, then COALESCE(foo) will return NULL

why would you use COALESCE(foo) when foo by itself gives the same result?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 08-21-09, 04:25
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 58
SQL-2003 (ISO/IEC 9075-2:2003 (E)):
6.11 <case expression>
[...]
<case abbreviation> ::=
NULLIF <left paren> <value expression> <comma> <value expression> <right paren>
| COALESCE <left paren> <value expression> { <comma> <value expression> }... <right paren>
[...]


I.e. two arguments or more.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On