1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ASP- SQL Server..Query help....

Discussion in 'C#' started by cancer10, Jan 30, 2007.

  1. #1
    Hello,

    I am using ASP and MS SQL Sever 2000 as backend database..

    I am writing a SQL query which will pick up top 5 recipes details who have scored the highest marks from the following table

    tblrecipes -

     ID |  recipe_name | Total  
    Code (markup):
    The following points to be noted:

    - The table has 500 records.
    - 50 of them have achieved the highest marks ie 100 marks each
    - My following query is picking up top 5 records who have scored 100 marks

    SELECT TOP 5 ID, total, recipe_name
    FROM tblrecipes 
    ORDER BY total DESC
    Code (markup):
    and getting this output:

    [​IMG]


    Now my question is:

    On what basis its showing up this sequence of listing? I mean why the ID 89 is on the top and 136 at the bottom? Why the ID is not the other way round?

    Like the following?

    ID
    __
    178
    136
    89
    286
    340


    Can someone explain please?


    Thanx
     
    cancer10, Jan 30, 2007 IP
  2. heck0045

    heck0045 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    SELECT TOP 5 ID, total, recipe_name
    FROM tblrecipes
    ORDER BY total DESC, ID desc

    If you want to put the highest IDs at the top

    You can NOT expect the IDs to be ordered in the arbitrary way you've laid them out:
    178
    136
    89
    286
    340

    You have to use something to order the IDs, either the ID itself or the recipe_name.
     
    heck0045, Jan 31, 2007 IP