SUM word COUNT in SQL Query - mysql

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;

Related

How to GROUP BY a string part in MySQL

EDIT:
Assuming you've got the following table:
id string number
1 stuff::a::312 5
2 stuff:::a::312 6
3 stuff::a::233 2
4 stuff:b::213 1
5 stuff::b::222 1
6 stuff::c 5
The following doesn't work of course:
SELECT string, COUNT(*)
FROM tbl
-- WHERE
GROUP BY string;
The wished result:
string numbers
a 13
b 2
c 5
Sorry, but please note that after c is no :: but before, just like the rest
If the pattern is same you can do something as
select
substring_index(string,'::',1) as string_val,
sum(number) as number
from mytable
group by string_val
you can do it with SUBSTRING_INDEX() like this:
SELECT string, COUNT(*)
FROM tbl
-- WHERE
GROUP BY SUBSTRING_INDEX(string, '::', 1);
Please try the following:
select substr(string,1,1)
, count(*)
from tbl
group by 1
;
Just use substr for this:
SELECT substr(string,1, 1), COUNT(*)
FROM tbl
-- WHERE
GROUP BY substr(string,1, 1);
Or more sophisticated SUBSTRING_INDEX:
SELECT SUBSTRING_INDEX(string, '::', 1), COUNT(*)
FROM tbl
-- WHERE
GROUP BY SUBSTRING_INDEX(string, '::', 1);
select left(string,1),count(*) from table where string is not null group by left(string,1) order by left(string,1)
I hope this helps,
LEFT(string) will only take left most character

Search within GROUP_CONCAT

I am executing this SQL from a big result of rows
SELECT userid, group_concat(locationid)
FROM user_location
group by userid
having group_concat(locationid) = 10
userid locationid
--------- ----------
894801 10,10,10,10,10,10,10,10,10,10,10,10
898356 10,10,11,10
900424 10,10,13,12,12,12,12
902123 10
904910 10,10
907922 10,10,10
912587 10,12,12
930319 10
Now, I want only those locationid rows where the value = 10 and no other value
Desired Output:
userid locationid
--------- ----------
894801 10,10,10,10,10,10,10,10,10,10,10,10
902123 10
904910 10,10
907922 10,10,10
930319 10
I explored and found find_in_set() but no use here
Don't use the result from group_concat(). Just use a simple having clause:
SELECT userid, group_concat(locationid)
FROM user_location
GROUP BY userid
HAVING SUM(locationid = 10) = COUNT(*)
The SUM() counts the number of values that are equal to 10. The = COUNT(*) simply says that all are 10.
You can also do this by being sure that no values are not 10:
HAVING SUM(locationid <> 10) = 0
SELECT * FROM user_location WHERE userid not in (select userid from user_location where locationid<>10)
group by userid having locationid=10;
Try this.. It's working

Count how many rows have the same value [duplicate]

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;

Sort varchar datatype with numeric characters

I have a varchar column that needs to be sorted by the number included in it. The data is similar to:
Group 1
Group 10
Group 11
Group 12
Group 13
Group 14
Group 15
Group 16
Group 17
Group 18
Group 19
Group 2
Group 20
Group 3
Group 4
Group 5
Group 6
Group 7
Group 8
Group 9
Test Group
I want the output like this where the value is sorted by the number.
Group 1
Group 2
Group 3
Group 4
Group 5…..
TRY like below, It will help you..
SELECT ColumnName FROM TableName
ORDER BY CONVERT(INT, Replace(ColumnName, 'Group',''))
SQL Fiddle : http://sqlfiddle.com/#!3/e14a6/7
Another Way
SELECT ColumnName FROM TableName
ORDER BY LEN(ColumnName),ColumnName
SQL Fiddle : http://sqlfiddle.com/#!3/e14a6/6
Another Way
SELECT ColumnName FROM TableName order by
case when
PATINDEX('%[^0-9]%',ColumnName) = 0
THEN
data
ELSE
cast(Left(ColumnName,PATINDEX('%[^0-9]%',ColumnName)-1) as int)
END
SQL Fiddle : http://sqlfiddle.com/#!3/14eb5/2
Another Way
Here i have add another solution by using Common Table Expression Try this...
with tempCTE(Data, pos)
as
(select data, Patindex('%[0-9]%', data) from sample),
tempCTE2(name, num)
as
(select SUBSTRING(data, 0, pos) name , cast(SUBSTRING(data, pos , LEN(data)) as int) num from tempCTE)
select name + CAST(num as varchar(10)) num1 from tempCTE2 order by name, num asc
SQL Fiddle : http://sqlfiddle.com/#!3/14eb5/3

Count duplicates records in Mysql table?

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;