Ask a Question related to ASP, Design and Development.
-
Ken Schaefer #1
Re: Problem using SELECT TOP and ORDER BY together in a query
SQL is based on set theory. You are ordering the resultset by the StoryDate.
So you have 6 articles at the top of the list. How is the database supposed
to work out which 3 of those you want? So, it returns all 6
Instead, if you add another column to the ORDER By clause, you can further
limit the number of records returned, eg:
FROM
tblNews
ORDER BY
StoryDate DESC
, ID DESC
Cheers
Ken
"Pooley" <pooley@here.com> wrote in message
news:%23EAU%23$HaDHA.1744@TK2MSFTNGP12.phx.gbl...
: Hi
:
: I have a large news-based database that I am selecting from but when using
: SELECT TOP and ORDER BY in the same query, ORDER BY is overriding the
SELECT
: TOP number.
:
: i.e.
:
: Query = "SELECT TOP 3 ID, Headline, StoryDate, Author, Body FROM tblNews
: ORDER BY StoryDate DESC"
:
: returns all *6* rows with the latest StoryDate in the tblNews table (as
does
: SELECT TOP 1 or 2 or anything under 6). Without the ORDER BY part of the
: query, I only get 3 rows returned but, of course, they are not the latest
: dated one.
:
: Any idea how I get around this one, please?
:
: Thank you.
:
:
Ken Schaefer Guest
-
Components, Query, Select Box Problem
:confused; I'm new to this.. and cannot for the life of me figure this out. Please help. This is the error I am receiving: Attribute... -
SELECT query Error, possible date problem?
Hello, I have what I thought was a fairly straight forward date based query that is returning an error I cant seem to get past. Here is my... -
problem with the select query
Hi people, this is my first mail to the group. i am having a problem with the select query. There are a few values in few columns of a table with... -
SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Following is my stored procedure. If I take the DISTINCT out then everything works fine. BUT I need the distinct because it returns duplicate... -
SELECT DISTINCT + ORDER BY gives ERROR 145: ORDER BY items mustappear in the select list if SELECT DISTINCT is specified.
Dan, You should be able to do this: SELECT Id, FaxID, ReceivedTime, Pages FROM ( SELECT DISTINCT .Id AS Id, -
Pooley #2
Re: Problem using SELECT TOP and ORDER BY together in a query
Gotcha. Thanks Ken.
"Ken Schaefer" <kenREMOVE@THISadOpenStatic.com> wrote in message
news:e$3LuuIaDHA.2336@TK2MSFTNGP09.phx.gbl...StoryDate.> SQL is based on set theory. You are ordering the resultset by thesupposed> So you have 6 articles at the top of the list. How is the databaseusing> to work out which 3 of those you want? So, it returns all 6
>
> Instead, if you add another column to the ORDER By clause, you can further
> limit the number of records returned, eg:
>
> FROM
> tblNews
> ORDER BY
> StoryDate DESC
> , ID DESC
>
> Cheers
> Ken
>
>
> "Pooley" <pooley@here.com> wrote in message
> news:%23EAU%23$HaDHA.1744@TK2MSFTNGP12.phx.gbl...
> : Hi
> :
> : I have a large news-based database that I am selecting from but whenlatest> : SELECT TOP and ORDER BY in the same query, ORDER BY is overriding the
> SELECT
> : TOP number.
> :
> : i.e.
> :
> : Query = "SELECT TOP 3 ID, Headline, StoryDate, Author, Body FROM tblNews
> : ORDER BY StoryDate DESC"
> :
> : returns all *6* rows with the latest StoryDate in the tblNews table (as
> does
> : SELECT TOP 1 or 2 or anything under 6). Without the ORDER BY part of the
> : query, I only get 3 rows returned but, of course, they are not the> : dated one.
> :
> : Any idea how I get around this one, please?
> :
> : Thank you.
> :
> :
>
>
Pooley Guest
-
Unregistered #3
Re: Problem using SELECT TOP and ORDER BY together in a query
Hi Guys,
I had the same problem and I splitted the request in two parts. In your case, you should have something like that:
SELECT TOP 3 ID
FROM tblNews
WHERE (ID IN (SELECT ID
FROM tblNews
ORDER BY StoryDate DESC))
I hope that'll solve your problem.
BuyUnregistered Guest



Reply With Quote

