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 , populate from diff tables based on condition

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-07, 07:07
shashi_s shashi_s is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
Select , populate from diff tables based on condition

I have 3 tables....
table 1: jobs
columns: job_id, customer_id, site_Id;
table 2: customers
columns: customer_id, address1, state, postcode
table3: addresses
columns: site_id, address1, state, postcode

My select statement should return job_id, address1, state, postcode
from jobs as j, customers as c, addresses as a.

If j.site_id > 1
then a.address1, a.state, a.postcode should be filled
else c.address1, c.state, c.postcode should be filled

any solution on how to do it?
Reply With Quote
  #2 (permalink)  
Old 04-10-07, 07:19
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
Does the platform you are using support the CASE statement?
__________________
Inspiration Through Fermentation
Reply With Quote
  #3 (permalink)  
Old 04-10-07, 07:24
shashi_s shashi_s is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
yes it does....
Reply With Quote
  #4 (permalink)  
Old 04-10-07, 07:30
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
CASE statements, as RedNeckGeek suggested, are SQL's version of an If statement.
The syntax is generally
Code:
CASE WHEN <criteria> THEN <output when true> ELSE <output when false> END
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 04-10-07, 07:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
CASE will not do the job here because it is a question of joining to one table or the other, and in order to use CASE as suggested you'd have to join to them both

there's a different approach:
Code:
select a.address1, a.state, a.postcode 
  from jobs
inner
  join addresses as a
    on a.site_id = jobs.site_id
 where jobs.site_id > 1
union all
select c.address1, c.state, c.postcode 
  from jobs
inner
  join customers as c
    on c.customer_id = jobs.customer_id
 where jobs.site_id <= 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 04-10-07, 07:47
RedNeckGeek RedNeckGeek is offline
Village Idiot
 
Join Date: Jul 2003
Location: Michigan
Posts: 1,941
nevermind.... I see the error in my ways.
Rudy wins again
__________________
Inspiration Through Fermentation

Last edited by RedNeckGeek; 04-10-07 at 08:01.
Reply With Quote
  #7 (permalink)  
Old 04-10-07, 07:53
shashi_s shashi_s is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
thanks a lot r937, yet to check if it gets the right data
Reply With Quote
  #8 (permalink)  
Old 04-10-07, 08:07
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Am I being foolish? Why wouldn't CASE work?
Because the data is in different tables?
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 04-10-07, 08:31
shashi_s shashi_s is offline
Registered User
 
Join Date: Apr 2007
Posts: 21
i have tried it with case....
but it only returns one row......which is the last one.
unless i've being trying it the wrong way
Reply With Quote
  #10 (permalink)  
Old 04-10-07, 08:32
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Re-read r937's post (#5) - he's the SQL king-thing...
If you can't get that to work then care to post your SQL statement?
__________________
George
Twitter | Blog
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