tags:

views:

622

answers:

6

I have a very very simple SQL string that MS Access doesn't seem to like.

Select Top 5 * From news_table Order By news_date

This is returning ALL rows from the news_table not just the Top 5.

I thought this was a simple SQL statement, apparently it is not.

Any fixes or idea as to why this is not working? Thanks!

A: 

Try Select Top 5 From news_table Order By news_date

That will not parse
TrickyNixon
he forgot the selector ( * ) :) but it will end up with the provided code
balexandre
+1  A: 

Select Top n selects all records where records are equal, that is, if you select Top 2 and you have 20 date1 and 20 date2, 40 records will be selected.

If there is a keyfield,

 Select Top 5 * Order By KeyField

Should return 5 records, because keyfield is unique.

Remou
So how could I limit it to only 2? I have tried as well to do Select * From news_table order by news_table Limit 2 and it does not work.
Bruno43
Do you have a key field? If you select top 2 order by keyfield, you will get 2, because the key is unique.
Remou
Only problem with this is, they have the ability to change the date, so I don't always want id 14 to be below id 15
Bruno43
I'm not too familiar with the intracacies of Access, but in SQL Server SELECT TOP X will only return X rows. It would only return ties if you specified that with SELECT TOP X WITH TIES. I'm not sure if Access is the same.
Tom H.
Jet SQL (Access has no SQL of its own) does not support WITH TIES -- TOP N always returns ties (http://office.microsoft.com/en-us/access/HP010322051033.aspx).
David-W-Fenton
+1  A: 

Does this do the trick?

select top 5 * 
from (select * from news_table order by news_date)

I don't know why the original doesn't work. Maybe it's a quirk with Access.

Edit: Business rules weren't specified. I didn't fully understand that the goal was to rank the table first and get the top 5 dates. It could have been to get the first 5 records and then rank them.

I can't reproduce your problem in my version of Access. Weird.

TrickyNixon
This works but your code is backwards. First we want to do the select with the Order to get the correct Order, then select the top 5. If you go with your code We would be selecting the top 5 first, then ordering those by news_date. But it helped. Thank you!
Bruno43
-1 wrong code, could mistake someone, please edit it :)
balexandre
Please change your code around and I will accept yours as the answer.
Bruno43
+1  A: 

I've had trouble in the past with similar statements within Access. To debug similar problems, I always break down the query into multiple queries. I would suggest creating a query within access that does the select with the order by statement. Then perform the select top from that query.

Query1 => SELECT * FROM news_table Order By news_date;

Query2 => Select Top 5 From Query1;

Sam
This is not necessary, or really useful in this case, I believe.
Remou
A: 

I hardly ever write SQL using *. I'm wondering if perhaps Jet SQL is not parsing that correctly. Have you tried:

SELECT TOP 5 news_table.* FROM news_table ORDER BY news_date

That would be the way I'd always write it.

Yes, "SELECT TOP 5 *" ought to work, but if it doesn't, perhaps a little more specificity would help.

--
David W. Fenton
David Fenton Associates

David-W-Fenton
A: 

Yes, This is working...

select top 5 *
from (select * from news_table order by news_date)

Ramu