Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2004, 01:04 PM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default SELECT query combining two field ids

I'm looking to combine two fields (ids) in a query that correspond to two different lookup tables in the database. I have this so far:

SELECT e.id, e.name, r.id, r.name
FROM
(select isnull(ethnicity_id, 3), isnull(race_id, 7), count(eir.id) as total
from enrollment_info_real eir)
join ethnicity e on (e.id = eir.ethnicity_id)
join race r on (r.id = eir.race_id)
group by ethnicity_id, race_id

I would like the select isnull... query to get me a data set that does not have nulls and then be able to join the lookup tables based on this data set. Any ideas on how to fix this? I keep getting the error message:

Server: Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'join'.

Thanks
 
Old September 29th, 2004, 02:29 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try changing:
(select isnull(ethnicity_id, 3), isnull(race_id, 7), count(eir.id) as total
from enrollment_info_real eir)

to

(select isnull(ethnicity_id, 3), isnull(race_id, 7), count(eir.id) as total
from enrollment_info_real) eir

That way eir is the alias for the subquery.

Brian
 
Old September 30th, 2004, 12:02 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

When u use a derived table to join, always an alias name is required.
it works but better to use Joins for all the tables instead of using derived tables. You don't have to use ISNULL in this case. Just u can use "Where T1.Col1 IS NOT NULL".

For example U have Table1 with col1,col2,col3
Table2 with Col1,col2,col3 and u want to display col2 from Table1 and col3 from table2 and u don't want the table2 to return null values.


SELECT T1.COL2,T2.COL3 FROM TABLE1 T1 INNER JOIN TABLE2 T2 ON
T1.COL1=T2.COL1
WHERE T2.COL2 IS NOT NULL


If u must a subquery as derived table, u can use IS NOT NULL any where. U can also use "COL2 <> NULL" provided that ANSI_NULLS is off.

Hope this helps.

B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a field using a SELECT statement? katie123 Access VBA 1 April 12th, 2006 10:23 AM
Select Query for Comma separated IDs veeruu SQL Language 3 February 9th, 2006 03:17 AM
Automatically select value from a calculated field jimmy0305 Access 0 June 21st, 2005 10:43 AM
can i select an access field umair_rathore Access 1 March 4th, 2005 08:53 AM
select query : * vs. Field list madhukp SQL Server 2000 16 September 10th, 2004 10:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.