473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Ranking records with how many times they exist

539 512MB
For example:

Cat
Dog
Cow
Rat
Cat
Cat
Rat
Cow
Cow
Cow

(RESULT)
Where it exist:
Cow 4 times
Cat 3 times
Rat 2 times
Dog 1 time

I would like to have a result like the one i mentioned above (RESULT)

How would it be queried like?

The current implementation has 2 queries, 1st is to select them distinctly, 2nd is one by one, each of the unique result will select again the db to count how many times it exist, i think it is a bad implementation and i hope it is possible to query once in the DB Server, any ideas?
Jan 6 '10 #1
3 2495
mwasif
802 Expert 512MB
This can be achieved by COUNT() and GROUP BY.

Expand|Select|Wrap|Line Numbers
  1. SELECT column_name, COUNT(*) as occurence FROM table_name
  2. GROUP BY column_name
  3. ORDER BY occurence DESC
Jan 6 '10 #2
nbiswas
149 100+
Try this

Expand|Select|Wrap|Line Numbers
  1. select Result = animals + '  ' +  CAST(COUNT(animals) as varchar(10))  + '  times'
  2. from AnimalTable
  3. group by animals 
  4. order by COUNT(animals) desc
Output:
Expand|Select|Wrap|Line Numbers
  1. Result
  2. Cow  4  times
  3. Cat  3  times
  4. Rat  2  times
  5. Dog  1  times
  6.  
Jan 7 '10 #3
sukatoa
539 512MB
Thank you for your replies experts. Effective =)
Jan 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Irene | last post by:
Hi all again, Well, I have my Athletics database with Athletes, Competitions, Scores tables. I have a ranking query where I get back the list of the competitions-athletes and scores...
5
by: ED | last post by:
I currently have vba code that ranks employees based on their average job time ordered by their region, zone, and job code. I currently have vba code that will cycle through a query and ranks each...
7
by: adm | last post by:
There are a few ways to go about this, but what is the fastest when called from VBA? This if for an ADP with a SQL Server back-end.
5
by: valglad | last post by:
Hi, The question below was posted about 4 years ago and noone was able to answer it back then. I have virtually the same type of problem so would appreciate if anyone can help. Thanks ...
4
by: Jim | last post by:
I have a form that is using a query for the control source. The query put the data in order of TOP 20. I've added an extra textbox in the form and want to be able to automatically rank the records...
6
by: sara | last post by:
I hope someone can help with this. Our director wants to have a report that will have the departments (Retail stores) across the top, stores down the side and the RANKING of the YTD dept sales...
5
by: Chris | last post by:
I was wodering if there was a way to rank numbers in a query like this #'s Rank 100 1 99 2 98 3 98 97 5 96 6 96
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
0
by: xahlee | last post by:
I have updated the computing sites popularity ranking, based on both alexa.com and quantcast.com. The whole report nicely formatted in HTML is here: http://xahlee.org/lang_traf/lang_sites.html ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.