0

I know how to set "top n", but if number of top depens on select result, I don't know how to set number of top.

for example(This works well):

select top 50 * from tbl order by id

I want to set as follows

select top (select count(*) from tbl where id < 50) * from tbl order by id

but this SQL is invalid.

Is there any solutions?

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Teppei Abe
  • 71
  • 1
  • 8
  • Does this answer your question? [Access 2010 Limit Query Results](https://stackoverflow.com/questions/24212290/access-2010-limit-query-results) – AStopher May 13 '20 at 07:34
  • 1
    I don't think this is possible. You'll have to create that SQL dynamically with VBA, and then assign it to a querydef. – Andre May 13 '20 at 08:01
  • @Andre is right. It can't be done dynamically in SQL. – Gustav May 13 '20 at 08:05
  • Thanks for your comment. I understood that it was impossible. – Teppei Abe May 13 '20 at 08:16
  • 2
    Maybe this is over my head but I really can't see how returning the count of records where ID<50 to use as TOP N is different from `SELECT * FROM table WHERE ID<50`. – June7 May 13 '20 at 10:39
  • I assume the example was (over-) simplified. @June7 – Andre May 13 '20 at 11:53

0 Answers0