我有以下查询,我正在寻找一个联接,根据id给我一个股票趋势的方向。
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
...
...
...
男人我很难加入!
非常感谢。
答案 0 :(得分:0)
这应该可行,但我认为您的查询可以优化
select stock_id, td.direction, MAX(timestamp) as latest_trend_date
from stock_trends st
join trend_direction td on st.direction_id = td.id
group by st.stock_id, td.direction
哦,是的,并将v.direction添加到主列表中。