Problem using SELECT TOP and ORDER BY together in a query

Ask a Question related to ASP, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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,
  3. #2

    Default 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...
    > 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.
    > :
    > :
    >
    >

    Pooley Guest

  4. #3

    Default 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.
    Buy
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139