I haven’t been able to solve this problem for several days now and I’m hoping you can help.
I’m trying to write a query that returns all the information about a stock and the last time it was updated. I would like to filter the results based on the parameter @date and return only the stocks which it’s latests update is less than the supplied @date parameter. I also need the stocks with a timestamp of null so I know that theses stocks need to be updated. I have the follwing three tables that I’m working with:
stocks
- id
- asset_id
- market_id
- name
- symbol
- IPOYear
- sector
- industry
updates
- id
- [timestamp]
stock_updates
- stock_id
- update_id
I’ve been using the following query and it was working well for me until I realized it dosen’t work if the stock doesn’t have an update
select * from stocks s
where @date < (
select top 1 u.timestamp from
updates u,
stock_updates su
where
s.id = su.stock_id and
u.id = su.update_id
order by u.timestamp desc
)
So after some research I came accross outer joins and I think it’s what I need to fix my problem I just haven’t been able to construct the correct query. The closest I’ve come is the following, but it returns a record for each time the stock was updated. Thanks in advance for your help!
This is where I’m at now:
select * from stocks s
left outer join stock_updates su on s.id = su.stock_id
left outer join updates u on u.id = su.update_id
where u.[timestamp] < @date
1 Answer