SQL select with counting rows

Hi there,
I have a table like the one below(simplified). I want to query the database to get the top 10 entries that have most number of "Numbers".  For example, in the table below 2345 appears 5 times, 1234 appears three times and 4546 appears two times.  I want to get the resultant set as given at the bottom.  What query command I can use?    Thank you.

name |ID| Number
abc | ID1 | 1234
wsx| ID6| 3456
wds| ID7| 4546
wdz| ID8| 2345
ert| ID12| 2345
cde | ID5| 1234
wfs| ID9| 2345
ert| ID13| 8955
ert| ID14| 4546
csg| ID11| 2345
ert| ID15| 9821
cdh | ID4| 1234
asd| ID10| 2345

The query results should be with total ten entries.  
wdz| ID8| 2345
ert| ID12| 2345
wfs| ID9| 2345
csg| ID11| 2345
asd| ID10| 2345
abc | ID1 | 1234
cde | ID5| 1234
cdh | ID4| 1234
wds| ID7| 4546
ert| ID14| 4546
ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Run this in SSMS to see if it meets your needs, then modify to fit your situation
CREATE TABLE #tmp (name varchar(5), ID varchar(5), Number int) 

INSERT INTO #tmp (name, ID, number)
VALUES
   ('abc' , 'ID1' , 1234), ('wsx', 'ID6', 3456), ('wds', 'ID7', 4546), 
   ('wdz', 'ID8', 2345), ('ert', 'ID12', 2345), ('cde' , 'ID5', 1234), 
   ('wfs', 'ID9', 2345), ('ert', 'ID13', 8955), ('ert', 'ID14', 4546), 
   ('csg', 'ID11', 2345), ('ert', 'ID15', 9821), ('cdh' , 'ID4', 1234), 
   ('asd', 'ID10', 2345)

SELECT t.Name, t.ID, t.Number
FROM #tmp t
   JOIN (SELECT Number, COUNT(Number) as number_count FROM #tmp GROUP BY Number) t_count ON t.Number = t_count.Number
ORDER BY t_count.number_count DESC

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ambuliAuthor Commented:
Thank you.  What does " t_count ON t.Number = t_count.Number" do
0
ambuliAuthor Commented:
By the way, I am using sqlite
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>Thank you.  What does " t_count ON t.Number = t_count.Number" do
I'm using a subquery named t_count to count the number of Numbes.
The JOIN .. ON joins that subquery to the main SELECT, so that I can use the count of Numbers in the ORDER BY clause.

>By the way, I am using sqlite
The code I posted was straight T-SQL, so if there's any differences between T-SQL and sqlite you'll have to explain for us, as I don't have that on my dev box.
0
ambuliAuthor Commented:
Thank you.  I got it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.