Count occurrences in string via MySQL - mysql

I want to count the occurrences of a string within the column "diagnosis".
What I am doing now is simply this - it gets my needed results
SELECT COUNT(*), diagnosis
FROM patients
WHERE diagnosis like "%OS1%"
UNION
SELECT COUNT(*), diagnosis
FROM patients
WHERE diagnosis like "%OS2%"
... and so on
Sometimes in my table the strings can occur twice (e.g. OS1, OS2), I want to count every single occurence of the strings.
I think it would be a pretty easy task in another language but I want to do it in pure SQL.

Put the OS1-OS6 and CH1-CH6 values in a diagn table. JOIN and GROUP BY:
SELECT COUNT(*), d.diagnosis
FROM patients p
RIGHT JOIN diagn d
on p.diagnosis like concat('%', d.diagnosis, '%')
group by d.diagnosis

SELECT sum(OS1) as OS1_total, sum(OS2) as OS2_total, sum(OS3) as OS3_total,
sum(OS4) as OS4_total, sum(OS5) as OS5_total, sum(OS6) as OS6_total,
sum(CH1) as CH1_total, sum(CH2) as CH2_total, sum(CH3) as CH3_total,
sum(CH4) as CH4_total, sum(CH5) as CH5_total, sum(CH6) as CH6_total
FROM
(
SELECT (case when diagnosis like '%OS1%' then 1 else 0 end) as OS1,
(case when diagnosis like '%OS2%' then 1 else 0 end) as OS2,
(case when diagnosis like '%OS3%' then 1 else 0 end) as OS3,
(case when diagnosis like '%OS4%' then 1 else 0 end) as OS4,
(case when diagnosis like '%OS5%' then 1 else 0 end) as OS5,
(case when diagnosis like '%OS6%' then 1 else 0 end) as OS6,
(case when diagnosis like '%CH1%' then 1 else 0 end) as CH1,
(case when diagnosis like '%CH2%' then 1 else 0 end) as CH2,
(case when diagnosis like '%CH3%' then 1 else 0 end) as CH3,
(case when diagnosis like '%CH4%' then 1 else 0 end) as CH4,
(case when diagnosis like '%CH5%' then 1 else 0 end) as CH5,
(case when diagnosis like '%CH6%' then 1 else 0 end) as CH6
FROM patients
) as mytable

You can try this also:
SELECT COUNT(*), diagnosis
FROM patients
GROUP BY diagnosis;

Not sure if this works.
SELECT COUNT(*)
FROM patients
WHERE REGEXP_LIKE(diagnosis, "(OS[1-6]|CH[1-6])")
You may also check out link below if you're interested. Not sure if it's what you're looking for though:
https://dev.mysql.com/doc/refman/8.0/en/regexp.html

Related

Is it good to select the same column on multiple where condition mysql?

I'm thinking about counting the number of rows depending on each condition and put each result with alias. I don't know if it possible and good to do it this way. For example:
select
(select sum(buy) from order_db where stts=1) as buy1,
(select sum(buy) from order_db where stts=2) as buy2,
(select sum(buy) from order_db where stts=3) as buy3
from order_db
where date='2021-08-29' and user='john'
I've tried but got error in return. I'm a basic sql learner. Please help me with suggestion or a clue to find the answer.
Use conditional aggregation:
SELECT SUM(CASE WHEN stts=1 THEN buy ELSE 0 END) buy1,
SUM(CASE WHEN stts=2 THEN buy ELSE 0 END) buy2,
SUM(CASE WHEN stts=3 THEN buy ELSE 0 END) buy3
FROM order_db
WHERE date='2021-08-29' AND user='john';
If there are other values than 1, 2 and 3 for stts you may also add to the WHERE clause:
AND stts IN (1, 2, 3)
So basically, this sounds like you are in need of a "conditional count". This can be achived with the combination of SUM and the CASE WHEN operator.
Applied to your problem, the solution would be somethinge like this:
select
(sum(case when stts = 1 then buy else 0 end)) as buy1,
(sum(case when stts = 2 then buy else 0 end)) as buy2,
(sum(case when stts = 3 then buy else 0 end)) as buy3
from order_db
where date='2021-08-29' and user='john'

Multiple Count with Multiple column

I am new in sql. I want to count something like:
Select count(*) from table where col1= x and col2=x and Col3=x.
I need to count the same value in all different column.
Any help will be appreciated.
You can use conditional aggregation :
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab;
If you want to have sum of these count values, consider the above query as an inner and use the following :
Select q.*,
q.count_col1 + q.count_col2 + q.count_col3 whole_sum
from
(
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab
) q
Rextester Demo

i want MySQL query result in same row

SELECT sum(case when (gender)=1 THEN 1 ELSE 0 END),
GROUP_CONCAT(sum(case when (gender)=2 THEN 1 ELSE 0 END) SEPARATOR ' ') as combine
from family_member_tbl
GROUP BY gender
NO, you can't nest grouping function like that. Rather get the sum first and then group_concat() like
select sum_1, sum_2, group_concat(sum_2) as combine
from (
SELECT gender,
sum(case when gender = 1 THEN 1 ELSE 0 END) as sum_1,
sum(case when gender = 2 THEN 1 ELSE 0 END) as sum_2
from family_member_tbl
GROUP BY gender ) xxx
group by gender;

SQL using CASE in count and group by

I'm using CASE to categorize data in the table and count them but the results aren't accurate
live demo [here]
select DATE(date) as day, count(*),
count(distinct case when name = 'fruit' then 1 else 0 end) as fruits,
count(distinct case when name = 'vege' then 1 else 0 end) as vege,
count(distinct case when name = 'sweets' then 1 else 0 end) as sweets
from food
group by day
with rollup
I'm not sure if the issue is with CASE or in the string matching = because there's no 'sweets' still it counts 1?
any pointers I'd be grateful
Your problem is that COUNT counts every result that is not NULL. In your case you are using:
COUNT(distinct case when name = 'sweets' then 1 else 0 end)
So, when the name is not sweets, it counts the 0. Furthermore, since you are using DISTINCT, it counts just one or two values. You should either use SUM or remove the DISTINCT and the ELSE 0:
SELECT DATE(date) as day,
COUNT(*),
SUM(CASE WHEN name = 'fruit' THEN 1 ELSE 0 END) as fruits,
SUM(CASE WHEN name = 'vege' THEN 1 ELSE 0 END) as vege,
SUM(CASE WHEN name = 'sweets' THEN 1 ELSE 0 END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Or:
SELECT DATE(date) as day,
COUNT(*),
COUNT(CASE WHEN name = 'fruit' THEN 1 ELSE NULL END) as fruits,
COUNT(CASE WHEN name = 'vege' THEN 1 ELSE NULL END) as vege,
COUNT(CASE WHEN name = 'sweets' THEN 1 ELSE NULL END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Here is a modified sqlfiddle.
You can't group by an alias. You have to group by the expression.
group by date(date)
You can group on an Alias:
SELECT
FROM_UNIXTIME(UnixTimeField, '%Y') AS 'Year'
,FROM_UNIXTIME(UnixTimeField, '%m') AS 'Month'
FROM table p
GROUP BY Year, Month

mysql views my query output i need different manner

This is my query
Select Count(Case When campaign_pid != 0 Then 1 End)
As Email, Count(Case When sms_pid != 0 Then 1 End)
As Sms, Count(Case When survey_pid != 0 Then 1 End)
As Survey From tablename
Please help me This is my query output
Email Sms Survey
21 1 4
i need output like this
name value
Email 21
Sms 1
Survey 4
Please tell me how to get like above optput
Something like the following should work
SELECT 'Email' AS Name, Count(Case When campaign_pid != 0 Then 1 End) As Value From tablename
UNION SELECT 'SMS', Count(Case When sms_pid != 0 Then 1 End)s From tablename
UNION SELECT 'Survey', Count(Case When survey_pid != 0 Then 1 End) From tablename
or better still
SELECT 'Email' AS Name, COUNT() AS Value FROM tablename WHERE campaign_pid <> 0
UNION SELECT 'SMS', COUNT() FROM tablename WHERE sms_pid <> 0
UNION SELECT 'Survey', COUNT() FROM tablename WHERE survey_pid <> 0
Not 100% sure of your additional question, maybe this is what you want
SELECT COUNT(DISTINCT some_unique_col)
FROM table_name
WHERE campaign_pid <> 0
AND sms_pid <> 0
AND survey_pid <> 0
Maybe you want SUM instead of COUNT, if you could provide some sample data and expected results from that it would help