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 > Double Left Join?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-03, 17:09
ScientificLee ScientificLee is offline
Registered User
 
Join Date: Sep 2003
Posts: 21
Double Left Join?

Helllo....


I need to do a query that left joins a table conditionally.

select t.*, v.vwap, a.moniker,v.dateTime
from accounts a, stocks s, trades t left join vwaps v on t.[date] = v.dateTime
where a.accountNumber = t.accountNumber AND t.symbol = s.symbol
AND a.moniker not like '%kb001%'
AND t.timeVWAP is not NULL
ORDER BY a.moniker, t.date, t.[transaction], t.symbol


but only left join vwaps if s.stockid = v.stockid

I used to have it like this.

select t.*, v.vwap, a.moniker,v.dateTime
from accounts a, trades t, stocks s left join vwaps v on s.stockID = v.stockID
where a.accountNumber = t.accountNumber AND t.symbol = s.symbol
AND a.moniker not like '%kb001%'
AND t.timeVWAP is not NULL
ORDER BY a.moniker, t.date, t.[transaction], t.symbol

in which case I would need to only join if v.dateTime = t.[date]
else just attach a NULL value.
Any idea how do to this?
Reply With Quote
  #2 (permalink)  
Old 09-16-03, 08:49
sandner sandner is offline
Registered User
 
Join Date: Nov 2002
Location: vienna
Posts: 9
Re: Double Left Join?

try this

select t.*, v.vwap, a.moniker,v.dateTime
from
accounts a,
(
stocks s
join
trades t
on t.symbol = s.symbol
)
left join
vwaps v on t.date = v.dateTime and s.stockid = v.stockid
where a.accountNumber = t.accountNumber
AND a.moniker not like '%kb001%'
AND t.timeVWAP is not NULL
ORDER BY a.moniker, t.date, t.transaction, t.symbol
Reply With Quote
  #3 (permalink)  
Old 09-17-03, 10:43
ScientificLee ScientificLee is offline
Registered User
 
Join Date: Sep 2003
Posts: 21
Will give it a try. Thanks.
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