Professional Web Applications Themes

Top 5 Query - Microsoft SQL / MS SQL Server

I am trying to run a query like the following: set rowcount 5 Select distinct col0 from tab0 where date between 'date1' and 'date2' sort by count(instances of returns in select statement) While this is a little oversimplified, I am trying to select all distinct rows, sort them by most to least and limit the returned rows to 5. MySQL has a function called "limit" where I could do this much easier, is there an easy way to accomplish this in MSSQL? Thanks, Casey...

  1. #1

    Default Top 5 Query

    I am trying to run a query like the following:

    set rowcount 5
    Select distinct col0 from tab0 where date between 'date1'
    and 'date2'
    sort by count(instances of returns in select statement)

    While this is a little oversimplified, I am trying to
    select all distinct rows, sort them by most to least and
    limit the returned rows to 5.

    MySQL has a function called "limit" where I could do this
    much easier, is there an easy way to accomplish this in
    MSSQL?

    Thanks,
    Casey

    Casey Guest

  2. #2

    Default Re: Top 5 Query

    You can use TOP clause in your SELECT statement with an ORDER BY clause. For
    example :

    SELECT TOP 10 col1, col2, ...
    FROM tbl
    ORDER BY col1;

    Refer to SQL Server Books Online for more details on TOP.

    --
    - Anith
    ( Please reply to newsgroups only )


    Anith Guest

  3. #3

    Default Re: Top 5 Query

    Casey, try:

    select top 5 col0
    from tab0
    where date between 'date1' and 'date2'
    group by col0
    order by count(*) desc

    --
    BG, SQL Server MVP
    Solid Quality Learning
    www.solidqualitylearning.com


    "Casey" <com> wrote in message
    news:032a01c35608$94e72810$gbl... 


    Itzik Guest

Similar Threads

  1. Creating data query from mySQL stored query!?
    By johnegbert in forum Coldfusion Database Access
    Replies: 2
    Last Post: August 5th, 09:28 PM
  2. Query of Queries on query New type query
    By david_h in forum Coldfusion Database Access
    Replies: 2
    Last Post: May 6th, 08:55 PM
  3. Replies: 1
    Last Post: July 2nd, 09:09 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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