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
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;
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;
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
I have 2 columns as follow:
A | B
---|---
7 | 1
7 | 2
3 | 7
4 | 5
-------
I want to get 1 column containing (1,2,3).
Currently i'm quering this:
SELECT `A` , `B`
FROM `mytable`
WHERE `A` =7
OR `B` =7
but I'm getting 2 columns containing the number 7 in both sides A and B.
i'm sure there is a way to get what I want but I don't know how to google that!!
You could use this:
SELECT
case when A=7 then B else A end
FROM
yourtable
WHERE
7 IN (A, B)
If you want a single column, you could use this:
SELECT
GROUP_CONCAT(case when A=7 then B else A end
ORDER BY case when A=7 then B else A end)
FROM
yourtable
WHERE 7 IN (A, B)
See fiddle here.
If value of both A and B could be 7 at the same time, and you want to skip that row, you could substitute:
WHERE 7 IN (A, B)
with
WHERE A=7 XOR B=7
Not really sure what you need, but I'm guessing something like this:
SELECT A FROM mytable WHERE B=7
UNION
SELECT B FROM mytable WHERE A=7
i think you looking for this
SELECT myresult from (
SELECT A myresult FROM Table1 WHERE A IN (1,2,3)
UNION
SELECT B myresult FROM Table1 WHERE B IN (1,2,3)
)t
ORDER BY myresult
this will out put :
MYRESULT
1
2
3
SQL DEMO HERE
Try this
SELECT A as Single FROM mytable WHERE B=7 UNION
SELECT B as Single FROM mytable WHERE A=7 ORDER BY Single;
If you are expecting result as 1, 2, 3 use below.
SELECT colB as NewCol FROM tableName WHERE colA=7
UNION
SELECT colA as NewCol FROM tableName WHERE colB=7;
If you want both columns simply use
SELECT colA, colB FROM tableName WHERE colA=7 OR colB=7;
Demo at sqlfiddle
you have to try this code..
SELECT b as value FROM users WHERE a='7'
UNION All
SELECT a as value FROM users WHERE b='7'
this query must help you.
I have the following query
SELECT Count(*) as Total_Count, Col1
FROM Table1
GROUP BY Col1
ORDER BY Total_Count DESC;
I want to zoom in on Col1. The data in Col1 are in the following format:
text-abc1
txt4-abcde22
tex6-abc2
text4-imp4
text-efg1
txt-efg43
I want to be able to group it by
After the first `-`, any first three/four/five characters match
In this example, if we match with first 3 characters. Output will be:
Total_Count Col1
3 abc
1 imp
2 efg
Any other way to achieve this?
You might not need a regex, just string operations. For three characters:
SELECT count(*) AS Total_Count,
SUBSTRING(Col1 FROM POSITION('-' in Col1)+1 FOR 3) AS Col1_zoomed
FROM Table1
GROUP BY Col1_zoomed
ORDER BY Total_Count DESC
select
substring(substring_index(col1,'-',-1),1,3) as grp,
count(*) as total
from table
group by grp
This should do what you want.
SELECT Count(*) as Total_Count, SUBSTRING(Col1, 1, 3)
FROM Table1
GROUP BY SUBSTRING(Col1, 1, 3)
ORDER BY Total_Count DESC;
I wanted to answer the question "MySQL GROUP by Regex?" as the answers here address the problem provided.
You can group by REGEXP with the REGEXP_SUBSTR() function.
REGEXP_SUBSTR(expr, pat[, pos[, occurrence[, match_type]]])
Returns the substring of the string expr that matches the regular
expression specified by the pattern pat, NULL if there is no match. If
expr or pat is NULL, the return value is NULL.
For example:
SELECT
*
FROM
YOUR_TABLE
GROUP BY REGEXP_SUBSTR(YOUR_COLUMN, 'YOUR REGEXP');
Reference: https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-substr
SELECT Count(*) as Total_Count, Col1, REGEXP_SUBSTR(Col1, '[a-z0-9]*', 0, 2) as Col1_combined
FROM Table1
GROUP BY REGEXP_SUBSTR(Col1, '[a-z0-9]*', 0, 2)
ORDER BY Total_Count DESC;
REGEXP_SUBSTR returns the string matching the given regular expression [a-z0-9] starting from position 0 in text and return the 2 occurrence. Documentation for REGEXP_SUBSTR
REGEXP_SUBSTR(<column_name>, <regular_expression>, <starting_position>, <match_occurrence>)