Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Top Ten; according to value?

Author  Topic 

jun0
Starting Member

32 Posts

Posted - 2013-01-22 : 14:17:54
Hi,

I guess this is a pretty basic question, but I was wondering how to list the "top ten" of a column (or "top 100" etc) according to the values in the column.

So for example if I have a customerid column with:

132
345
1
45
234
34
23
56
67
98
45
67
98
67
56
54
57
58
345
34
5678

how do i select * for the the "top ten" highest values in that column?

Hope you don't mind my question, thanks.

ssunny
Posting Yak Master

133 Posts

Posted - 2013-01-22 : 14:35:26
select top 10 * from yourtable order by customerid desc
Go to Top of Page

cstokes91
Yak Posting Veteran

72 Posts

Posted - 2013-01-22 : 14:49:38
As ssunny said
Select Top 10 *
FROM tableName
Order by CustomerID desc

... and that should work if customerID is an int
if it is varchar (trust me I've seen it all) then just throw a cast in there

Order by cast(customerID as int) desc and it should work like a charm.
Go to Top of Page

jun0
Starting Member

32 Posts

Posted - 2013-01-22 : 14:55:08
Thanks guys,

cstokes91, I've seen it all too, I'm still new to SQL (as you can tell) but I was thrown in rather at the deep end with a DB I inherited, and there are many columns that should have been of data types they aren't, including int's that were varchars. :)
Go to Top of Page

cstokes91
Yak Posting Veteran

72 Posts

Posted - 2013-01-22 : 16:58:55
I feel your pain man... I graduated from college Spring 2012 with a degree in Management Info Systems and once you actually start getting into this stuff it is a whole different world.

My specific job is especially challenging because I do data transfers from clients data to our system so not only do I have to fully know how our system works but I am constantly learning other people's system to get their data over. I hear it gets easier but I haven't seen it yet ;)

I sure do learn something new EVERY day, though.
Go to Top of Page
   

- Advertisement -