我有以下查詢,我正在尋找一個連接,以給我一個基於id的股票趨勢的方向。SQL加入以獲取字段名稱
stock_trends
------------
stock_id
trend_id
direction_id
timestamp
price
breakout_price
trend_direction
---------------
id
direction
select s.*, v.latest_trend_date,
dbo.GetStockAverageVolume(s.id, latest_trend_date, GETDATE())
as avg_volume from stocks s
join(select stock_id, MAX(timestamp)as latest_trend_date from stock_trends st
group by st.stock_id) v on v.stock_id = s.id
where
(select top 1 trend_id from stock_trends
where s.id = stock_trends.stock_id order by [timestamp] desc) =
@trend_id and s.market_id = @market_id
and dbo.GetStockAverageVolume(s.id, latest_trend_date, GETDATE()) > 300000
order by latest_trend_date desc
如何修改上述查詢以獲取基於stock_trends表內direction_id的趨勢方向?
例如:
select s.*, v.latest_trend_date,
dbo.GetStockAverageVolume(s.id, latest_trend_date, GETDATE())
as avg_volume, **direction** from stock s
...
...
...
人我在加入壞了!
非常感謝。
你正在使用哪種SQL? – Hogan 2012-04-02 04:05:22
@Hogan MSSQL 2008 – robson 2012-04-02 04:06:54