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 > General > Database Concepts & Design > A joining table situation

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-26-03, 18:50
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
A joining table situation

Here is my situation:

table A
id int primary key
...

table A1
id int reference A on delete cascade,
code char(2),
...
primary key (id, code)

where multiple entries with the same id but different code may exist in table A1.

Now, is possible to have only one item per id with a selection statement something like the followings:

select a.id, ... from A a join A1 a1 using (id) where code in ('en', 'es')

And ideally, returning the entry with the code 'en' if there is one.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-26-03, 19:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
select A.id, ...
     , min(A1.code)
  from A 
inner  
  join A1
    on A.id = A1.id
 where A1.code in ('en', 'es')
group
    by A.id, ...
you must group by every "..." column in the SELECT list

rudy
http://r937.com/
Reply With Quote
  #3 (permalink)  
Old 12-26-03, 19:37
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
You are quick, Rudy.

The query works if the select columns only from the table A. Unfortunately, I have some columns from the A1. What I try to do is to eliminate duplicate returned result (in terms of same ID) with a bias to one code value.
Reply With Quote
  #4 (permalink)  
Old 12-26-03, 19:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you shoulda said so to start with
Code:
select A.id
     , FOO.BAR
     , FOO.code
  from A 
inner  
  join A1 as FOO
    on A.id = FOO.id
 where FOO.code =
       ( select min(code)
           from A1
          where id = FOO.id
            and code in ('en', 'es')
       )
rudy
Reply With Quote
  #5 (permalink)  
Old 12-26-03, 20:28
Vernon Vernon is offline
Registered User
 
Join Date: Jul 2003
Posts: 24
Sorry, I didn't know what would be involved.

It works. I only provide an example data of the code. Say the code 'en' is the one I preferred, but it is not necessary the smallest one in any given list of codes. Any further enhacnement possibility?
Reply With Quote
  #6 (permalink)  
Old 12-26-03, 20:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, but it gets messier and messier
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