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
Related
what is the difference between those 2 queries below?
========The first query==========
SELECT 1 AS question_id, COUNT(id) AS answer
FROM annual_payments
UNION
SELECT 2 AS question_id, COUNT(DISTINCT user_id) AS answer
FROM annual_payments
UNION
SELECT 3 AS question_id, COUNT(id) AS answer
FROM annual_payments
WHERE STATUS = "paid" AND amount >= 100
UNION
SELECT 4 AS question_id, product AS answer
FROM annual_payments
WHERE status = 'paid'
ORDER BY SUM(amount)
LIMIT 1
When I executed this query, I got the error:
Error 1054: Unknown column 'amount' in 'order clause'
==========The second query===========
SELECT 1 AS question_id, COUNT(id) AS answer
FROM annual_payments
UNION
SELECT 2 AS question_id, COUNT(DISTINCT user_id) AS answer
FROM annual_payments
UNION
SELECT 3 AS question_id, COUNT(id) AS answer
FROM annual_payments
WHERE STATUS = "paid" AND amount >= 100
UNION
SELECT *
FROM
(SELECT 4 AS question_id, product AS answer
FROM annual_payments
WHERE status = 'paid'
ORDER BY amount
LIMIT 1 ) x
=========================
I got the answer with the second query.
The difference between those 2 queries is to use "select *"
Why I got an error with the 1st query? What is the difference between those 2?
When you combine multiple queries with union, the column aliases are determined by the first query.
An order by clause applies to the entire result set and must refer to the columns as defined by the first query in the set of unioned queries.
Your second example works because you are not applying order by to the unioned results, you are using it within the context of a derived table which is fine, it applies to that sub-query only.
I have table that looks like this:
id rank
a 2
a 1
b 4
b 3
c 7
d 1
d 1
e 9
I need to get all the distinct rank values on one column and count of all the unique id's that have reached equal or higher rank than in the first column.
So the result I need would be something like this:
rank count
1 5
2 4
3 3
4 3
7 2
9 1
I've been able to make a table with all the unique id's with their max rank:
SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id
I'm also able to get all the distinct rank values and count how many id's have reached exactly that rank:
SELECT
DISTINCT TopRank AS 'rank',
COUNT(id) AS 'count of id'
FROM
(SELECT
MAX(rank) AS 'TopRank',
id
FROM myTable
GROUP BY id) tableDerp
GROUP BY TopRank
ORDER BY TopRank ASC
But I don't know how to get count of id's where the rank is equal OR HIGHER than the rank in column 1. Trying SUM(CASE WHEN TopRank > TopRank THEN 1 END) naturally gives me nothing. So how can I get the count of id's where the TopRank is higher or equal to each distinct rank value? Or am I looking in the wrong way and should try something like running totals instead? I tried to look for similar questions but I think I'm completely on a wrong trail here since I couldn't find any and this seems a pretty simple problem that I'm just overthinking somehow. Any help much appreciated.
One approach is to use a correlated subquery. Just get the list of ranks and then use a correlated subquery to get the count you are looking for:
SELECT r.rank,
(SELECT COUNT(DISTINCT t2.id)
FROM myTable t2
WHERE t2.rank >= r.rank
) as cnt
FROM (SELECT DISTINCT rank FROM myTable) r;
I am trying to count the number of profile visits, but it counts the wrong number. In the following example there should be 3 visits, but it counts 6! Anyone know what is wrong with it? http://sqlfiddle.com/#!9/b43ea/8
SELECT *,
COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
LEFT JOIN user_login ON user_login.user_id = profile_visitors.user_id
WHERE profile_visitors.user_id = 1
You need to Group By to count multiple rows, So take the Star out of your query and add a group by user_id also make it profile_visitors.*
The LEFT JOIN to user_login table provides no benefit to this question, but, the following query will get you the detail you want to see (assuming you only want to see the number of visits for user_id = 1):
SELECT COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
WHERE profile_visitors.user_id = 1
GROUP BY profile_visitors.profile_id
To see all visits by profile use:
SELECT profile_id, COUNT(profile_visitors.profile_id) AS visitorCount
FROM profile_visitors
GROUP BY profile_visitors.profile_id
you can use WHERE IN () to compare if profile_visitors.user_id exist in user_login
SELECT *,
COUNT(profile_visitors.user_id) AS visitorCount
FROM profile_visitors
WHERE profile_visitors.user_id IN (SELECT user_id FROM user_login )
result:
id user_id profile_id visit_date visitorCount
1 1 1 May, 10 2015 15:26:46 3
This seems to be easy in my head but I can't figure how to write the query:
What I have:
user_id correct_questions
1 5
1 2
2 3
2 1
What I want to have:
user_id correct_questions(sum)
1 7
2 4
The only thing I get is how often user x has played.
(with count(correct_questions) GROUP BY user_id)
Well only thing you have to do is SUM + group by :
select user_id, SUM(correct_questions) as correct_questions
from yourtable
Group by user_id
select sum(correct_questions) from table_name group by user_id
What you need is SUM() function and not COUNT() as correctly pointed by "Grodon Linoff"
select sum(correct_questions) as correct
from table
group by user_id
select user_id, sum(correct_questions) from tb_name group by user_id
While I wrote this, 10 others wrote the same. Thanks!
I just answered my own question within the question.
Of course I have to use sum() instead of count()
SELECT *, sum(`correct_answers`) as sum
FROM `user_quiz_rel`
GROUP BY `u_id`
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