April 28, 2008 at 2:14 pm
Hi All,
I need a bit of help with a join. I have 2 tables :
TradeSummary
has fields : SymbolID, CurrentPrice, TotalValue
Trades
has fields : SymbolID, TradeID, ExecutionTime, TradeValue
TradeSummary has one entry for each SymbolID, while Trades contains one or more entries per SymbolID
and what I want to retreive is :
For every item in TradeSummary get CurrentPrice, TotalValue from TradeSummary
and also get TradeValue from Trades for the record for max(ExecutionTime)
tables are joined on TradeSummary.SymbolID = Trades.SymbolID
Every attempt of mine so far returns multiple rows for each SymbolID - I want only one row per SymbolID
thanks in advance
April 28, 2008 at 3:49 pm
How about
SELECT ts.SymbolID, MAX(t.ExecutionTime) FROM TradeSummary ts LEFT JOIN Trades t ON ts.SymbolID=t.SymbolID GROUP BY t.SymbolID
April 28, 2008 at 4:01 pm
If SQL Oracle's answer does not do it for you, you'll want to give us some more info such as sample data, expected results, and how 'bout posting the queries that you've tried?
http://www.sqlservercentral.com/articles/Best+Practices/61537/
April 29, 2008 at 6:16 am
Finally got it to work, here's the correct sql - I am using the unique+increment field Trades.ID to return the newest Trade for a given SymbolID :
select a.SymbolID, a.CurrentPrice, a.TotalValue, t1.TradeValue, t1.Exec_Time
from TradeSummary a, Trades t1
where a.SymbolID = t1.SymbolID
and t1.ID in (select MAX(t2.ID) from Trades t2
where a.SymbolID = t2.SymbolID)
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply