SQL select with counting rows

Hi there,
It is a followup to one of my previous question, re- posted below.  The given solution works.  But, instead of limiting it to 10 entries all together I want to limit 3 distinct numbers.  For example,  2345, 1234, 4546.  So, that even if "2345" appears 20 times, it will still list all the three distinct numbers.  (Count after group by and list max 3 groups)


http://www.experts-exchange.com/questions/28705605/SQL-select-with-counting-rows.html

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.

Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can try this query

SELECT TOP 10 *,(SELECT COUNT(NUMBER) FROM TVIK T WHERE T.number = O.number) RN FROM TVIK O
ORDER BY RN DESC

Open in new window

0
Julian HansenCommented:
Try this
SELECT tableName.*, a.Total 
	FROM tableName LEFT JOIN 
	(
		SELECT COUNT(*) AS Total, Number 
		FROM tableName 
		GROUP BY Number 
		ORDER BY COUNT(*) 
		DESC LIMIT 3 
	) a
	ON tableName.Number = a.Number 
	WHERE a.Number IS NOT NULL
	ORDER BY Total DESC;

Open in new window

0
PortletPaulfreelancerCommented:
Is this SQLite like the previous question?

Julian: why use a left join and filter for matches, an inner join would do it automatically
0
ambuliAuthor Commented:
Yes, it is sqlite.
0
Julian HansenCommented:
@Paul - no reason other than sometimes answering a question is done with half brain focused on something else. INNER JOIN makes more sense
SELECT tableName.*, a.Total 
	FROM tableName INNER JOIN 
	(
		SELECT COUNT(*) AS Total, Number 
		FROM tableName
		GROUP BY Number 
		ORDER BY COUNT(*) 
		DESC LIMIT 3 
	) a
	ON tableName.Number = a.Number 
	ORDER BY Total DESC;

Open in new window

1

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
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.