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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Top Ten; according to value?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jun0
Starting Member

United Kingdom
32 Posts

Posted - 01/22/2013 :  14:17:54  Show Profile  Reply with Quote
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.

Edited by - jun0 on 01/22/2013 14:18:49

ssunny
Posting Yak Master

USA
133 Posts

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

cstokes91
Yak Posting Veteran

USA
72 Posts

Posted - 01/22/2013 :  14:49:38  Show Profile  Reply with Quote
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

United Kingdom
32 Posts

Posted - 01/22/2013 :  14:55:08  Show Profile  Reply with Quote
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

USA
72 Posts

Posted - 01/22/2013 :  16:58:55  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.13 seconds. Powered By: Snitz Forums 2000