Ordering by a maximum value

# Thread: Ordering by a maximum value

1. Senior Member
Join Date
Dec 1969
Posts
626

## Ordering by a maximum value

I have 3 records in a database...<BR>i need to order the records by their hit counts value...<BR>how do i do this<BR><BR>select max(hits) as totals from table<BR><BR>that is not working for me

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Ordering by a maximum value

select hits from table order by hits desc<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
417

## RE: Ordering by a maximum value

What you&#039;re doing will select the record with the largets value in the "hits" column. I think you want:<BR><BR>"SELECT hits FROM table ORDER BY hits"<BR><BR>... which will select the hits value in order, greatest first, (to do the opposite order, write DESC at the end)

4. Senior Member
Join Date
Dec 1969
Posts
626

## RE: Ordering by a maximum value

that still isn&#039;t working...<BR><BR>actually, i have 10 records in the table, but i only want the top 3 records with the most hits...

5. Senior Member
Join Date
Dec 1969
Posts
417

## RE: Ordering by a maximum value

aaawww! you beat me by a seconds!

6. Senior Member
Join Date
Dec 1969
Posts
417

## RE: Ordering by a maximum value

can&#039;t you select them all, but only use the top 3? Loop while a counter variable is less than 3?

7. Senior Member
Join Date
Dec 1969
Posts
626

## my exact statement

ads.Source = "SELECT top 5 * FROM ads where (dateadded &#060;= &#039;" & date & "&#039;) AND (datetoend &#062; &#039;" & date & "&#039;) order by hits desc"<BR><BR>it just doesn&#039;t wanna go... :-(

8. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Ordering by a maximum value

select top 3 hits from table order by hits desc

9. Senior Member
Join Date
Dec 1969
Posts
626

## DA!

hits column was nvarchar not numeric!

#### Posting Permissions

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