Home > front end >  How to count and sort occurances of entries in a column in SQL?
How to count and sort occurances of entries in a column in SQL?

Time:11-13

I have a column in my table completed named completed at (with a space). It contains either a blank entry, or value1, value2, value3.

I would like to run a SQL query that counts the amounts of time value1, value2, value3 etc. occurs, ordered by descending order.

I would like it to look like this:

Count Location
49 value2
18 value1
1 value3

Is this possible to do?

CodePudding user response:

For ANSI SQL I would write this. If you need other kind of SQL please specify and I will glade to update my answer.

SELECT 
   COUNT(*) as Count,
   "completed at" as Location
FROM yourtable
GROUP BY "completed at" 
ORDER BY 1 DESC

CodePudding user response:

Put the column name in square brackets so you don't have to worry about empty spaces.

SELECT 
   COUNT(*) AS Count,
   [completed at] AS Location
FROM tablename
GROUP BY [completed at]
ORDER BY Count DESC
  •  Tags:  
  • sql
  • Related