How to Count Number of Occurrences for All Different Values in Database Column

How to count number of occurrences for all different values in database column?

Group by the column you are interested in and then use count to get the number of rows in each group:

SELECT column5, COUNT(*)
FROM table1
GROUP BY column5

How to count occurrences of a column value efficiently in SQL?

This should work:

SELECT age, count(age) 
FROM Students
GROUP by age

If you need the id as well you could include the above as a sub query like so:

SELECT S.id, S.age, C.cnt
FROM Students S
INNER JOIN (SELECT age, count(age) as cnt
FROM Students
GROUP BY age) C ON S.age = C.age

Count the occurrences of DISTINCT values

SELECT name,COUNT(*) as count 
FROM tablename
GROUP BY name
ORDER BY count DESC;

Count number of occurrences for each unique value

Yes, you can use GROUP BY:

SELECT time,
activities,
COUNT(*)
FROM table
GROUP BY time, activities;

How to get the count of each distinct value in a column?

SELECT
category,
COUNT(*) AS `num`
FROM
posts
GROUP BY
category

How do I count occurrences of distinct data in two columns from 1 table MySql

SELECT name, COUNT(*)
FROM ( SELECT firstname name
FROM meeting
UNION ALL
SELECT secondname
FROM meeting ) x
GROUP BY name;

Counting occurrences of unique values in a column using sql

SELECT ColumnName, COUNT(*)
FROM TableName
GROUP BY ColumnName

SQL Count occurrences in a column and calculate total of another

you can try like below , remove distinct

SELECT
THESTATUS,
COUNT(THENAME),
SUM(THECOST)
FROM THETABLE
GROUP BY thestatus

demo

T-SQL : count occurrences per unique substring in a column

You should keep the extracted 3 letters in the select statement and Group by.

Query

Select left(code, 3) as code3, count(left(code, 3)) as cnt
From tablename
Group by left(code, 3)
Order by 1;

Count number of occurrences of a column in another column

This works for the given data:

select t1.ID, t1.type, count(t2.type) as `count`
from table1 t1
left join table1 t2
on t2.type = 'fruits'
and t2.reference like concat('%"', t1.ID, '"%')
where t1.`type` <> 'fruits'
group by t1.ID, t1.type;

Result:

|  ID |  type | count |
|-----|-------|-------|
| 206 | apple | 2 |
| 212 | apple | 1 |
| 217 | apple | 0 |
| 360 | apple | 1 |

http://sqlfiddle.com/#!9/2a790/4

There are many crazy things you can do with SQL - but it doesn't mean that you should.



Related Topics



Leave a reply



Submit