I know this has a stupid solution but, sorry, I'm little bit confused.
I have two SELECT COUNT statements. Example:
Statement 1
SELECT COUNT(softwareone) AS totalcount
FROM my_table WHERE softwareone LIKE '%typeone%'
totalcount = 3
_
Statement 2
SELECT COUNT(softwaretwo) AS totalcount
FROM my_table WHERE softwaretwo LIKE '%typeone%'
totalcout = 1
I want to sum both totals to get totalcount = 4. There is a way to do that?
Note: software type from columns "softwareone" and "softwaretwo" is of the same type (same value).
Thanks to all.
One way is to write:
SELECT SUM(CASE WHEN softwareone LIKE '%typeone%'
AND softwaretwo LIKE '%typeone%'
THEN 2
ELSE 1
END
) AS "totalcount"
FROM my_table
WHERE softwareone LIKE '%typeone%'
OR softwaretwo LIKE '%typeone%'
;
The CASE ... END expression will evaluate to 2 when both conditions are met (so that if softwareone and softwaretwo are both LIKE '%typeone%', then the row counts twice), and 1 when only one of them is. So, the SUM(CASE ... END) gives the total number of rows where the one condition is met, plus the total number of rows where the other condition is met.
You could use a
Select l1.totalcount + l2.totalcount FROM
(SELECT COUNT(softwareone) AS totalcount
FROM my_table WHERE softwareone LIKE '%typeone%') as l1,
(SELECT COUNT(softwaretwo) AS totalcount
FROM my_table WHERE softwaretwo LIKE '%typeone%') as l2
Related
I made query like this:
SELECT DISTINCT TahunMasuk (SELECT COUNT(LamaStudi)
FROM studi WHERE LamaStudi < 2) from studi order by TahunMasuk
but, the query is error. What may I do to solve that query so that I can count LamaStudi based by TahunMasuk? The result example is like this
Here is my table
studi
Thanks in advance
This query should return the result you want:
SELECT TahunMasuk,
COUNT(CASE WHEN LamaStudi < 2 THEN 1 END) AS `LamaStudi < 2`,
COUNT(CASE WHEN LamaStudi BETWEEN 2 AND 2.4 THEN 1 END) AS `LamaStudi < 2-2.4`,
COUNT(CASE WHEN LamaStudi > 2.4 THEN 1 END) AS `LamaStudi > 2.4`,
FROM studi
GROUP BY TahunMasuk;
In your sample data, you have some rows where LamaStudi is NULL, but it's not clear how you want to handle that. If you want NULL to be counted as zero, then you need this small modification:
COUNT(CASE WHEN COALESCE(LamaStudi,0) < 2 THEN 1 END) AS `LamaStudi < 2`,
You were using DISTINCT incorrectly. The DISTINCT option applies to all columns in the select-list.
The following rows are all distinct rows:
a b c
a b d
a e d
DISTINCT keeps a row in the result if any column is different from other rows.
If you want to reduce the result set to rows with distinct values in one column, use GROUP BY.
You can use GROUP BY:
SELECT TahunMasuk, COUNT(*)
FROM studi
GROUP BY TahunMasuk
This is my first post, so any general corrections to format/content are also welcome. I'm relatively new to SQL.
Say I have a database which collects test results from an classification evaluation. I know what the expected outcome is for each test. I also have a column indicating whether the test was successful, ie the expected value returned matched the expected value. It looks something like this:
Expected_Result Result Success
A A True
A B False
B B True
A A True
B A False
I know I can return the total occurrences of each expected type withSELECT Expected_Result, COUNT(Expected_Result) FROM Evaluation_Results GROUP BY Expected_Result.
I know how to count the number of false detections for a specific expected outcome with SELECT COUNT(*) FROM Evaluation_Results WHERE Success = 'True' AND Expected_Result = 'A'
Where I'm struggling is combining the two. I would like the query to return a list of all distinct expected results, the total of each, the count of successful results, and the percentage of the total, like so:
Expected_Result Total Num_Successful Success_Rate
A 3 2 66.67
B 2 1 50.00
You could use a CASE expression to perform a condition check during aggregation. A case statement identifies a conditional outcome. For instance you could use:
select evaluation_result
, count(*) AS total
, sum(case when success='true' and result='a' then 1 else 0 end) AS num_successful
, sum(case when success='true' and result='a' then 1 else 0 end)/count(*) AS success_rate
from evaluation_results group by evaluation_result;
Basically what's happening there is you're taking a count(*) of all grades, a sum() of a 1 or 0 based on a conditional outcome, then performing the ratio math. There's no need for a join here. The CASE Expression is a powerful conditional statement which can be used in so many diverse ways.
Or for a more flexible solution have a look at this:
select evaluation_result
, count(*) AS total
, sum(case when success='true' and result=evaluation_result then 1 else 0 end) AS num_successful
, sum(case when success='true' and result=evaluation_result then 1 else 0 end)/count(*) AS success_rate
from evaluation_results group by evaluation_result;
You can use self join if table is same like.
SELECT distinct e.Expected_Result, COUNT(Expected_Result), sum(e1.columns name), avg(e1.column name)
FROM Evaluation_Results e
left join Evaluation_Results e1 on e1.col=e.col
GROUP BY e.Expected_Result
Use this simple Query and check for the result..
select Expected_Result, count(Expected_Result) Total,
sum(IF ('True' = Success, 1, 0) ) Num_Successful,
avg(IF ('True' = Success, 1, 0 )) Success_Rate
from Evaluation_Results group by Expected_Result
I have a mysql table(tbl_subscriptiondetails) like the above.Here the last column i.e
'test_status' could have possible values C,S & Y only.
Using a single query i would like to fetch data in the manner
as shown below
But the query that i'm using gives me undesired results.My query is
SELECT pkg_name,
(SELECT COUNT(*) FROM tbl_subscriptiondetails WHERE test_status='C' AND mem_id=3) AS completed,
(SELECT COUNT(*) FROM tbl_subscriptiondetails WHERE test_status='S' AND mem_id=3) AS started,
(SELECT COUNT(*) FROM tbl_subscriptiondetails WHERE test_status='Y' AND mem_id=3) AS remaining
FROM tbl_subscriptiondetails
WHERE mem_id=3
GROUP BY pkg_name
and the result is
Please advise what is wrong. Thanks in advance.
You are running a subquery for each count, instead of counting as part of the main query. Therefore, your filter conditions aren't behaving as you expect them to. You can change it like so, to do the conditional count:
SELECT pkg_name,
SUM( case when test_status='C' then 1 else 0 end) AS completed,
SUM( case when test_status='S' then 1 else 0 end) AS started,
SUM( case when test_status='Y' then 1 else 0 end) AS remaining
FROM tbl
WHERE mem_id = 3
GROUP BY pkg_name
Demo
I am looking to find the count of rows where the entered answer is the same as the correct answer. Here's an example:
WorkerID Answer Correct
1 A A
1 B C
2 A D
I would then get the following result:
WorkerID AnswerCount # Correct
1 2 1
2 1 0
So far I have (conceptually):
SELECT worker_id, count(*), count(Answer == Correct) FROM answer_table GROUP BY WorkerID
What would be the correct query here?
You don't want count(), you want sum():
SELECT worker_id, count(*) as AnswerCount, sum(Answer = Correct) as NumCorrect
FROM answer_table
GROUP BY WorkerID;
count() counts the number of non-NULL values that the expression takes on. You want to count the number of matches, which is the number of trues.
I think this is what you want :
select count(*)
from yourTable
where answer = correct
group by workerId
Basically, what you need to do is
Select all where answer = correct.
group them by workerId.
count the num of rows (where answer = correct) for each group.
Edit : To answer to your edited question,
select count(*), count(b.workerId)
from yourTable
left join (select *
from yourTable
where answer = correct) b using(workerId)
group by workerId
use this:
select workerid,count(*) as numberOfAnswers,
sum(case
when answer=correct then 1
else 0 end) as correctAnswers
from tbl
group by workerid
DEMO
Wondering is there is a way to write the following in ONE MySQL query.
I have a table:
cust_ID | rpt_name | req_secs
In the query I'd like to get:
the AVG req_secs when grouped by cust_ID
the AVG req_secs when grouped by rpt_name
the total req_secs AVG
I know I can do separate grouping queries on the same table then UNION the results into one. But I was hoping there was some way to do it in one query.
Thanks.
Well, the following would does two out of three:
select n,
(case when n = 1 then cast(cust_id as varchar(255)) else rpt_name end) as grouping,
avg(req_secs)
from t cross join
(select 1 as n union all select 2
) n
group by n, (case when n = 1 then cust_id else rpt_name end);
This essentially "doubles" the data and then does the aggregation for each group. This assumes that cust_id and rpt_name are of compatible types. (The query could be tweaked if this is not the case.)
Actually, you can get the overall average by using rollup:
select n,
(case when n = 1 then cust_id else rpt_name end) as grouping,
avg(req_secs)
from t cross join
(select 1 as n union all select 2
) n
group by n, (case when n = 1 then cast(cust_id as varchar(255)) else rpt_name end) with rollup
This works for average because the average is the same on the "doubled" data as for the original data. It wouldn't work for sum() or count().
No there is not. You can group by a combination of cust_ID and rpt_name at the same time (i.e. two levels of grouping) but you are not going to be able to do separate top-level groupings and then a non-grouped aggregation at the same time.
Because of the way GROUP BY works, the SQL to do this is a little tricky. One way to get the result is to get three copies of the rows, and group each set of rows separately.
SELECT g.gkey
, IF(g.grp='cust_id',t.cust_ID,IF(g.grp='rpt_name',t.rpt_name,'')) AS gval
, AVG(t.req_secs) AS avg_req_secs
FROM (SELECT 'cust_id' AS gkey UNION ALL SELECT 'rpt_name' UNION ALL SELECT 'total') g
CROSS
JOIN mytable t
GROUP
BY g.gkey
, IF(g.grp='cust_id',t.cust_ID,IF(g.grp='rpt_name',t.rpt_name,''))
The inline view aliased as "g" doesn't have to use UNION ALL operators, you just need a rowset that returns exactly 3 rows with distinct values. I just used the UNION ALL as a convenient way to return three literal values as a rowset, so I could join that to the original table.