Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Order By problem

    I have a table with a 'rank' column. This column is use to record the top 3
    ranked records - ie the user enters a 1,2,3 or blank against each record.

    I want to list the top three in order, followed by the non-ranked records in
    any order.

    A further complication is that this must work for Access 2k and SQL Server
    2k..

    Any ideas?

    Thanks

    Chris


    CJM Guest

  2. Similar Questions and Discussions

    1. 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...
    2. XML import order problem
      Hi All, I'm using the xml connector to display an xml file in a datagrid but the order of the columns is going alphabetically rather than the...
    3. Textures order problem in Shockwave
      I am getting a problem with the order textures are shown in Shockwave and was wondering what I need to do to solve the problem. For example, when...
    4. order of event firing problem
      Hi all, Would really appreciate some help on this one! Am trying to make a multilingual site with a button to toggle between English and...
    5. Order problem
      A have never seen such a thing before: A query result order is not proper when I use "order by" clause. SQL> select * from mytable order by name;...
  3. #2

    Default Re: Order By problem

    CJM wrote:
    > I have a table with a 'rank' column. This column is use to record the
    > top 3 ranked records - ie the user enters a 1,2,3 or blank against
    > each record.
    >
    > I want to list the top three in order, followed by the non-ranked
    > records in any order.
    >
    > A further complication is that this must work for Access 2k and SQL
    > Server 2k..
    >
    > Any ideas?
    >
    > Thanks
    >
    > Chris
    You could use a union query:

    select 'ranked' as Source, rank,<rest of fields>
    from table where rank is not null
    Union all
    select 'unranked' as Source, rank,<rest of fields>
    from table where rank is null
    Order By Source, rank

    HTH,
    Bob Barrows


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.


    Bob Barrows [MVP] Guest

  4. #3

    Default Re: Order By problem

    Thanks Bob.

    I haven't actually got it working yet, but I've developed some SQL based on
    you suggestion that seem to do the job I want.

    Tx

    Chris

    "Bob Barrows [MVP]" <reb01501@NOyahoo.SPAMcom> wrote in message
    news:OXeGFf89DHA.4020@TK2MSFTNGP09.phx.gbl...
    > CJM wrote:
    > > I have a table with a 'rank' column. This column is use to record the
    > > top 3 ranked records - ie the user enters a 1,2,3 or blank against
    > > each record.
    > >
    > > I want to list the top three in order, followed by the non-ranked
    > > records in any order.
    > >
    > > A further complication is that this must work for Access 2k and SQL
    > > Server 2k..
    > >
    > > Any ideas?
    > >
    > > Thanks
    > >
    > > Chris
    >
    > You could use a union query:
    >
    > select 'ranked' as Source, rank,<rest of fields>
    > from table where rank is not null
    > Union all
    > select 'unranked' as Source, rank,<rest of fields>
    > from table where rank is null
    > Order By Source, rank
    >
    > HTH,
    > Bob Barrows
    >
    >
    > --
    > Microsoft MVP -- ASP/ASP.NET
    > Please reply to the newsgroup. The email account listed in my From
    > header is my spam trap, so I don't check it very often. You will get a
    > quicker response by posting to the newsgroup.
    >
    >

    CJM Guest

  5. #4

    Default Re: Order By problem

    <<
    I have a table with a 'rank' column. This column is use to record the
    top 3
    ranked records - ie the user enters a 1,2,3 or blank against each
    record.

    I want to list the top three in order, followed by the non-ranked
    records in
    any order.

    A further complication is that this must work for Access 2k and SQL
    Server
    2k..

    Any ideas?

    Thanks

    Chris
    >>
    How about something like this:

    SELECT * FROM MyTable ORDER BY IsNull(Rank) DESC, Rank

    Best regards,
    J. Paul Schmidt, Freelance ASP Web Developer
    [url]http://www.Bullschmidt.com[/url]
    ASP Design Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...


    *** Sent via Developersdex [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Bullschmidt Guest

  6. #5

    Default Re: Order By problem

    Bob,

    I've got your solution working with SQL Server, but not with Access.

    My SQL query is:

    Select 'Ranked' as Source, H.HotelName, H.TelNo, H.URL, H.Location,
    H.HotelID, H.Rank
    from (Hotels H Inner Join PrefHotels P on H.HotelID = P.HotelID)
    Inner Join Locations L on P.LocID = L.LocID
    where L.LocID = 2 and H.rank is not null
    Union
    Select 'Unranked' as Source, H.HotelName, H.TelNo, H.URL, H.Location,
    H.HotelID, H.Rank
    from (Hotels H Inner Join PrefHotels P on H.HotelID = P.HotelID)
    Inner Join Locations L on P.LocID = L.LocID
    where L.LocID = 2 and (H.Rank is Null or H.Rank = 0)
    Order By Source, H.Rank

    For context, I am linking a table of hotels with a table of locations. The
    PrefHotels table links the hotels to appropriate locations (ie allows a
    many-to-many relationship)

    With Access, I get the following error:

    'The ORDER BY expression (H.Rank) includes fields that are not selected by
    the query. Only those fields requested in the first query can be included in
    an ORDER BY expression.'

    Any ideas?

    Thansk

    Chris


    CJM Guest

  7. #6

    Default Re: Order By problem

    CJM wrote:
    > Bob,
    >
    > I've got your solution working with SQL Server, but not with Access.
    >
    > My SQL query is:
    >
    > Select 'Ranked' as Source, H.HotelName, H.TelNo, H.URL, H.Location,
    > H.HotelID, H.Rank
    > from (Hotels H Inner Join PrefHotels P on H.HotelID = P.HotelID)
    > Inner Join Locations L on P.LocID = L.LocID
    > where L.LocID = 2 and H.rank is not null
    > Union
    > Select 'Unranked' as Source, H.HotelName, H.TelNo, H.URL, H.Location,
    > H.HotelID, H.Rank
    > from (Hotels H Inner Join PrefHotels P on H.HotelID = P.HotelID)
    > Inner Join Locations L on P.LocID = L.LocID
    > where L.LocID = 2 and (H.Rank is Null or H.Rank = 0)
    > Order By Source, H.Rank
    >
    > For context, I am linking a table of hotels with a table of
    > locations. The PrefHotels table links the hotels to appropriate
    > locations (ie allows a many-to-many relationship)
    >
    > With Access, I get the following error:
    >
    > 'The ORDER BY expression (H.Rank) includes fields that are not
    > selected by the query. Only those fields requested in the first query
    > can be included in an ORDER BY expression.'
    >
    > Any ideas?
    >
    Yes. Don't use the table alias in the ORDER By clause. With a union query,
    the only fields the "order by" knows about are the field names coming from
    the first select statement. It knows nothing about the data sources in that
    select statement. So go back to my initial suggestion and simply say:

    Order By Source, Rank

    This will work in both Access and SQL Server

    Bob Barrows
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  8. #7

    Default Re: Order By problem

    Bullschmidt wrote:
    > <<
    >
    > A further complication is that this must work for Access 2k and SQL
    > Server
    > 2k..
    >
    >
    > How about something like this:
    >
    > SELECT * FROM MyTable ORDER BY IsNull(Rank) DESC, Rank
    >
    IsNull, while it is a VBA function, does not do the same thing as the SQL
    Server ISNULL() function.

    Bob Barrows


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Barrows Guest

  9. #8

    Default Re: Order By problem

    OK, Bob... I've taken your suggestion...

    I'm now getting an error on the next line:

    "No value given for one or more required parameters."
    on
    "Set rsInfo = oConn.Execute (sSQL)"

    [This occurs for the Access DB version only]

    Any further ideas?

    Cheers




    CJM Guest

  10. #9

    Default Re: Order By problem

    If it's any use, I've just tried entering the SQL that my ASP produces into
    a query in Access itself, and it works... so the problem is presumably with
    ASP/ADO rather than Access/SQL...

    CJM

    "CJM" <cjmwork@yahoo.co.uk> wrote in message
    news:OD9mOIj%23DHA.2808@TK2MSFTNGP10.phx.gbl...
    > OK, Bob... I've taken your suggestion...
    >
    > I'm now getting an error on the next line:
    >
    > "No value given for one or more required parameters."
    > on
    > "Set rsInfo = oConn.Execute (sSQL)"
    >
    > [This occurs for the Access DB version only]
    >
    > Any further ideas?
    >
    > Cheers
    >
    >
    >
    >

    CJM Guest

  11. #10

    Default Re: Order By problem

    <! #include file="expletives.inc">

    Er... I still had a H.Rank in the query somewhere... oops!

    All seems to be working.. finally.

    Thanks

    Chris

    "CJM" <cjmwork@yahoo.co.uk> wrote in message
    news:OWtRqMj%23DHA.2064@TK2MSFTNGP11.phx.gbl...
    > If it's any use, I've just tried entering the SQL that my ASP produces
    into
    > a query in Access itself, and it works... so the problem is presumably
    with
    > ASP/ADO rather than Access/SQL...
    >
    > CJM
    >
    > "CJM" <cjmwork@yahoo.co.uk> wrote in message
    > news:OD9mOIj%23DHA.2808@TK2MSFTNGP10.phx.gbl...
    > > OK, Bob... I've taken your suggestion...
    > >
    > > I'm now getting an error on the next line:
    > >
    > > "No value given for one or more required parameters."
    > > on
    > > "Set rsInfo = oConn.Execute (sSQL)"
    > >
    > > [This occurs for the Access DB version only]
    > >
    > > Any further ideas?
    > >
    > > Cheers
    > >
    > >
    > >
    > >
    >
    >

    CJM 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