This question already has answers here:
Count the occurrences of DISTINCT values
(4 answers)
Closed 1 year ago.
How do I write an SQL query to count the total number of a specific num value in the num column of a table?
Assuming we have the following data.
NAME
NUM
SAM
1
BOB
1
JAKE
2
JOHN
4
Take the following query:
SELECT WHERE num = 1;
This would return these two rows.
NAME
NUM
SAM
1
BOB
1
Try
SELECT NAME, count(*) as NUM FROM tbl GROUP BY NAME
SQL FIDDLE
If you want to have the result for all values of NUM:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
GROUP BY `NUM`
Or just for one specific:
SELECT `NUM`, COUNT(*) AS `count`
FROM yourTable
WHERE `NUM`=1
FOR SPECIFIC NUM:
SELECT COUNT(1) FROM YOUR_TABLE WHERE NUM = 1
FOR ALL NUM:
SELECT NUM, COUNT(1) FROM YOUR_TABLE GROUP BY NUM
SELECT
COUNT(NUM) as 'result'
FROM
Table1
GROUP BY
NUM
HAVING NUM = 1
Try this Query
select NUM, count(1) as count
from tbl
where num = 1
group by NUM
--having count(1) (You condition)
SQL FIDDLE
SELECT sum(num) WHERE num = 1;
SELECT SUM(IF(your_column=3,1,0)) FROM your_table WHERE your_where_contion='something';
e.g. for you query:-
SELECT SUM(IF(num=1,1,0)) FROM your_table_name;
Use this query this will give your output:
select
t.name
,( select
count (*) as num_value
from Table
where num =t.num) cnt
from Table t;
Related
I have a table with 2 fields:
cnt str
-- -------
60 the
58 of
4 no
30 the
2 of
1 no
and i want result like this
cnt str
-- -------
90 the
60 of
5 no
How would I write a query to like below the table?
SELECT str,
SUM (cnt)
FROM table_name
GROUP BY str;
This will group the table by str, ie all the will be together and then summed and so on.
if you want to rename the sum(cnt)
use:
SELECT str,
SUM (cnt) as cnt
FROM table_name
GROUP BY str;
Use GROUP BY:
select sum(cnt) as cnt, str from my_table group by str
this will work indeed because it is suming the cnt grouping by str as you wanted:
select sum(cnt),str from tablename group by str;
I have a table like this
I want to check the all rows in Column A with column B and get the count of duplicates.
For example, I want to get the
count of 12 as 3(2 times in A+1 time in B)
count of 11 as 2(2 times in A+0 time in B)
count of 13 as 2(1 time in A+0 time in B)
How can I acheive it?
You can calculate the total occurrences from a union all. A where clause can show only the values that occur in the A column:
select nr
, count(*)
from (
select A as nr
from YourTable
union all
select B
from YourTable
) sub
where nr in -- only values that occur at least once in the A column
(
select A
from YourTable
)
group by
nr
having count(*) > 1 -- show only duplicates
You can combine all values in A and B then do the group by.
Then only select those values found in column A.
Select A, count(A) as cnt
From (
Select A
from yourTable
Union All
Select B
from yourTable) t
Where t.A in
(select distinct A from yourTable)
Group by t.A
Order by t.A;
Result:
A cnt
11 2
12 3
13 1
See demo: http://sqlfiddle.com/#!9/9fcfe9/3
I have a table with the following structure:
id name
1 X
1 X
1 Y
2 A
2 A
2 B
Basically what I am trying to do is to write a query that returns X for 1 because X has repeated more than Y (2 times) and returns A for 2. So if a value occurs more than the other one my query should return that. Sorry if the title is confusing but I could not find a better explanation. This is what I have tried so far:
SELECT MAX(counted) FROM(
SELECT COUNT(B) AS counted
FROM table
GROUP BY A
) AS counts;
The problem is that my query should return the actual value other than the count of it.
Thanks
This should work:
SELECT count(B) as occurrence, A, B
FROM table
GROUP BY B
ORDER BY occurrence DESC
LIMIT 1;
Please check: http://sqlfiddle.com/#!9/dfa09/3
You can try like this using a GROUP BY clause. See a Demo Here
select *, max(occurence) as Maximum_Occurence from
(
select B, count(B) as occurence
from table1
group by B
) xxx
This is how I finally handled my problem. Not the most efficient way but get the job done:
select A,B from
(select A,B, max(cnt) from
(select A ,B ,count(B) as cnt
from myTable
group by A,B
order by cnt desc
) as x group by A
) as xx
m new in mysql
here is my table
now i want to count "count_id" where count of 'questionID' greater than 2
Try this :
SELECT COUNT(count_id) FROM myTable WHERE questionID > 2
select count(Count_ID),QuestionID,SurveyId from table
where QuestionID>2
group by QuestionID,SurveyID
select count(count_id) from yourtable where questionID > 2
If you want to count unique ID:
select count(DISTINCT count_id) from table_name where questionID > 2
SELECT COUNT(count_id) FROM table_name WHERE questionID > 2
Group by Count_ID and count their distinct questions. Stay with those that have more than two. Then count how many IDs you got.
select count(*)
from
(
select count_id
from mytable
group by count_id
having count(distinct questionid) > 2
) x;
EDIT: If count_id + questionid happen to be unique for the table, you can replace count(distinct questionid) with count(*).
You can also try the statement below:
select count(count_id) CountOfID,count_id from mytable
where questionID > 2 group by count_id;
I have table with, folowing structure.
tbl
id name
1 AAA
2 BBB
3 BBB
4 BBB
5 AAA
6 CCC
select count(name) c from tbl
group by name having c >1
The query returning this result:
AAA(2) duplicate
BBB(3) duplicate
CCC(1) not duplicate
The names who are duplicates as AAA and BBB. The final result, who I want is count of this duplicate records.
Result should be like this:
Total duplicate products (2)
The approach is to have a nested query that has one line per duplicate, and an outer query returning just the count of the results of the inner query.
SELECT count(*) AS duplicate_count
FROM (
SELECT name FROM tbl
GROUP BY name HAVING COUNT(name) > 1
) AS t
Use IF statement to get your desired output:
SELECT name, COUNT(*) AS times, IF (COUNT(*)>1,"duplicated", "not duplicated") AS duplicated FROM <MY_TABLE> GROUP BY name
Output:
AAA 2 duplicated
BBB 3 duplicated
CCC 1 not duplicated
For List:
SELECT COUNT(`name`) AS adet, name
FROM `tbl` WHERE `status`=1 GROUP BY `name`
ORDER BY `adet` DESC
For Total Count:
SELECT COUNT(*) AS Total
FROM (SELECT COUNT(name) AS cou FROM tbl GROUP BY name HAVING cou>1 ) AS virtual_tbl
// Total: 5
why not just wrap this in a sub-query:
SELECT Count(*) TotalDups
FROM
(
select Name, Count(*)
from yourTable
group by name
having Count(*) > 1
) x
See SQL Fiddle with Demo
The accepted answer counts the number of rows that have duplicates, not the amount of duplicates. If you want to count the actual number of duplicates, use this:
SELECT COALESCE(SUM(rows) - count(1), 0) as dupes FROM(
SELECT COUNT(1) as rows
FROM `yourtable`
GROUP BY `name`
HAVING rows > 1
) x
What this does is total the duplicates in the group by, but then subtracts the amount of records that have duplicates. The reason is the group by total is not all duplicates, one record of each of those groupings is the unique row.
Fiddle: http://sqlfiddle.com/#!2/29639a/3
SQL code is:
SELECT VERSION_ID, PROJECT_ID, VERSION_NO, COUNT(VERSION_NO) AS dup_cnt
FROM MOVEMENTS
GROUP BY VERSION_NO
HAVING (dup_cnt > 1 && PROJECT_ID = 11660)
I'm using this query for my own table in PHP, but it only gives me one result whereas I'd like to the amount of duplicate per username, is that possible?
SELECT count(*) AS duplicate_count
FROM (
SELECT username FROM login_history
GROUP BY username HAVING COUNT(time) > 1
) AS t;