-1

I cant find how to get the 5 last prices for all actions I'm following. Here is the DB :

ISIN table :

 mnemonic | name | toFollow

Prices table

mnemonic | date | price

How can I get the 5 last prices for each ISIN where toFollow = 1 ?

Thanks for your help

Shadow
  • 33,525
  • 10
  • 51
  • 64
Mr Jenkins
  • 27
  • 6
  • Include sample data set relevant to your question with proper table definitions – M Khalid Junaid Nov 27 '17 at 13:10
  • use order by and LIMIT 5 – apomene Nov 27 '17 at 13:10
  • This is such a common question on SO that it has its own tag called `greatest-n-per-group`. The two linked questions provide lots of different ways to get the results you are looking for. Pick one based on your requirements. – Shadow Nov 27 '17 at 13:20
  • This should help https://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ – SALEH Nov 27 '17 at 13:27

1 Answers1

-2

Just need to order by date and then limit to 5:

select a.mnemonic,b.date,b.price
from isin as a
     join isin b on (a.mnemonic=b.mnemonic)
where a.tofollow=1
order by b.date
limit 5;
nacho
  • 5,280
  • 2
  • 25
  • 34