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 > SELECT query combining two field ids

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-04, 14:00
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
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
Reply With Quote
  #2 (permalink)  
Old 09-29-04, 14:47
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
wow, that's too confusing for me

in the outer query, you cannot have non-aggregate columns in the SELECT list which aren't in the GROUP BY

in the inner query, you can't have both aggregate and non-aggregate columns in the SELECT list if you don't have a GROUP BY

the derived table alias eir goes outside the parentheses

i have no clue what you're trying to do but perhaps this will point you in the right direction --
Code:
select ???
  from enrollment_info_real 
inner
  join ethnicity e 
    on coalesce(enrollment_info_real.ethnicity_id,3)
     = e.id
inner
  join race r 
    on coalesce(enrollment_info_real.race_id,7)
     = r.id 
group 
    by ???
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-30-04, 11:23
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
What does coalesce do?
Reply With Quote
  #4 (permalink)  
Old 09-30-04, 11:36
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
Does this look right if I wanted to make a grid of sorts combining a person's race with ethnicity? I want to show how many people are white/hispanic or latino, white/non-hispanic or latino, etc.

select count(e.id), e.name, count(r.id), r.name
from enrollment_info_real
inner
join ethnicity e
on coalesce(enrollment_info_real.ethnicity_id,3)
= e.id
inner
join race r
on coalesce(enrollment_info_real.race_id,7)
= r.id
group
by e.id, e.name, r.id, r.name
Reply With Quote
  #5 (permalink)  
Old 09-30-04, 11:50
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
coalesce finds a non-null value within its list of values

so coalesce(enrollment_info_real.race_id,7) means that if enrollment_info_real.race_id is null, then 7 is used

as for your grouping, i think you need to learn about grouping and understand how it works

you probably want
Code:
select e.name, r.name, count(*) as total
...
group by e.name, r.name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 09-30-04, 12:09
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
Does this look right if I wanted to make a grid of sorts combining a person's race with ethnicity? I want to show how many people are white/hispanic or latino, white/non-hispanic or latino, etc.

select count(e.id), e.name, count(r.id), r.name
from enrollment_info_real
inner
join ethnicity e
on coalesce(enrollment_info_real.ethnicity_id,3)
= e.id
inner
join race r
on coalesce(enrollment_info_real.race_id,7)
= r.id
group
by e.id, e.name, r.id, r.name
Reply With Quote
  #7 (permalink)  
Old 09-30-04, 12:59
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
I think I have it right:

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

This would create the grid but now I am looking for a way to sum the columns on the bottom of the grid and the rows on the right hand side of the grid. Any ideas?
Reply With Quote
  #8 (permalink)  
Old 09-30-04, 13:10
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
your GROUP BY is still wrong

have you been testing these queries?

what database is this?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-30-04, 13:44
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
Yes the totals all check out since I did manual queries on the database to test the results of the query I'm working on. What is wrong with the group by clause?
Reply With Quote
  #10 (permalink)  
Old 09-30-04, 13:48
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
Is it because I had the e.id column in my group by clause?
Reply With Quote
  #11 (permalink)  
Old 09-30-04, 13:50
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
my mistake, i thought you were counting e.id, you're actually counting eir.id

what database is this?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 09-30-04, 13:59
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
It's a database with the main table being a list of demographics of people signing up for a certain service. There are lots of lookup tables (race, ethnicity, etc.)

So how could I sum the bottom and right hand side of the grid? And btw, thanks for your help
Reply With Quote
  #13 (permalink)  
Old 09-30-04, 14:37
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
"what database" means is it access? mysql? sybase? db2? informix? freebird?

i'm sorry, i don't know what you mean by "grid"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #14 (permalink)  
Old 09-30-04, 14:45
funkedup funkedup is offline
Registered User
 
Join Date: May 2004
Posts: 15
sorry, SQL Server 2000. By grid I mean:


-------------Race Race Race Total
Ethnicity 8 2 5 ?

Ethnicity 7 10 8 ?

Ethnicity 6 15 2 ?

Total ? ? ? ?


I'm trying to figure out how to get the totals.
Reply With Quote
  #15 (permalink)  
Old 09-30-04, 14:49
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
to get totals at the end of the line, you would add another term to the SELECT list

however, in your case, this doesn't make sense, because you are only selecting one aggregate amount per line

to get a total line, in general, you would use UNION ALL and a separate query for the totals

i just don't see how your query produces a grid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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