Results 1 to 12 of 12

Thread: top 20

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    i just wwant to retrieve top 20 records based on number of hits
    something like SQL = "SELECT * FROM MY TABLE where hits >0 TOP 20"
    whats best way of getting the 20 records with a WHERE clause

  2. #2
    Hyperactive Member barrk's Avatar
    Join Date
    Sep 2000
    Location
    My own little world
    Posts
    274
    select top 20 <selectlist> from mytable where <hitfield> > 20

    top n[percent] is also acceptable if you aren't looking for only 20 records but want to limit the size of your output.

  3. #3

  4. #4
    Hyperactive Member barrk's Avatar
    Join Date
    Sep 2000
    Location
    My own little world
    Posts
    274

    Smile

    You are welcome!!!!!!!!!

  5. #5
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ..

    can i use that in sql server 6.5

    can't seem to get it to work

    Think I am missing something

    getting the error

    incorrect syntax near 10

    when I do
    select top 10 from tblone

  6. #6
    Hyperactive Member barrk's Avatar
    Join Date
    Sep 2000
    Location
    My own little world
    Posts
    274
    You need "select top 10 whatevers(insert you fieldname here) from tableone" in SQL 7.0

  7. #7
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    ...Thanks.. do not have 7.0 in production..

    Thanks

  8. #8
    Hyperactive Member barrk's Avatar
    Join Date
    Sep 2000
    Location
    My own little world
    Posts
    274
    Did you try adding the fieldnames to your SQL statement and running it on SQL 6.5? I'd give that a try before I gave up!

  9. #9
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    TOP will return a fairly arbitrary set of records that satisfy the criteria unless you also use ORDER BY.

    For instance, say you want the five records with most hits and hits > 5.

    e.g.
    ID Hits
    1 10
    2 5
    3 4
    4 7
    5 6
    6 12
    7 18
    8 2
    9 23
    10 1

    "SELECT TOP 5 ID FROM MyTable WHERE HITS > 5" might return ID's 1, 5, 4, 9 and 7 - ID 6 should be included but you have not specified an order, so the SELECT will simply return the first 5 records it finds.

    "SELECT TOP 5 ID FROM MyTable WHERE HITS > 5 ORDER BY Hits" will return
    9, 7, 6, 1, 4 - That's better.

    Incidentally, note that TOP will not distinguish between ties, so if the table was:

    ID Hits
    1 10
    2 7
    3 4
    4 7
    5 7
    6 12
    7 18
    8 2
    9 23
    10 1

    "SELECT TOP 5 ID FROM MyTable WHERE HITS > 5 ORDER BY Hits" will return
    9, 7, 6, 1, 2, 4, 5 - seven records, despite requesting 5! What you are really saying is up to 5 and ties.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    dont worry

    i already had ORDER BY
    most of my sql statements got one of those hehe

  11. #11
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    I believe you.

    Cheers,

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  12. #12
    Fanatic Member SkiNLaB's Avatar
    Join Date
    Jan 2002
    Location
    Sydney, Australia
    Posts
    747
    well i need to use TOP 100 and i NEED an order by but i DONT want ties!!! stupid ties! any help?

Posting Permissions

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



Click Here to Expand Forum to Full Width