Combining two queries from the same table? - mysql

I have a MySQL table that keeps records of users that answered a question, with the number of trials. It's like:
username trials
-------- ------
user1 10
user2 7
user1 20
etc. From which I can calculate how many times they answered a question (COUNT).
and now I want to calculate average number of trials, i.e. get the following table:
username avg
-------- ---
user1 15
user2 7
I've tried this query:
SELECT(a.totalguess/b.totalknow) as avg FROM( SELECT username, SUM(trials) AS totalguess FROM thetable GROUP BY username) a, (SELECT username, COUNT(*) as totalknow FROM thetable GROUP BY username)b WHERE a.username=b.username;
and it gave only
avg
---
15
7
pretty close! but without the knowers. I could probably combine them with php, but I want a pure MySQL solution. So what should I do?
Thanks in advance!

SELECT username as knower, AVG(trials) as theavg
FROM mytable
GROUP BY username
will give you this:
knower, theavg
user1, 15
user2, 7

SELECT a.knower, (a.totalguess/b.totalknow) as avg
FROM (SELECT knower, SUM(trials) AS totalguess FROM thetable GROUP BY knower) a,
(SELECT knower, COUNT(*) as totalknow FROM thetable GROUP BY knower)b
WHERE a.knower=b.knower;
or simply:
SELECT knower, SUM(trials)/COUNT(*) as avg -- or you can just use AVG(trials)
FROM thetable
GROUP BY knower

You can do this in one query:
SELECT knower, SUM(trials)/count(*)
from thetable
group by knower

Your initial SELECT statement determines what columns will be returned. Right now you have
SELECT(a.totalguess/b.totalknow) as avg FROM ...
So you will only get one column called "avg" back. So add the knower to your select list.
SELECT a.knower as knower,
(a.totalguess/b.totalknow) as avg
FROM
...

SELECT knower, AVG(trials) FROM thetable GROUP BY knower

Related

SQL Query to count(x) and group by y

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`

MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?

I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;

Condition for counting distinct rows in an SQL query

I have a table in a MySQL database with an ID column. This is not a key of the table and several rows can have the same ID.
I don't really know SQL but I already figured out how to obtain the number of distinct IDs:
SELECT COUNT(DISTINCT ID) FROM mytable;
Now I want to count only those IDs which appear more than 2 times in the table.
So if the ID column contains the values
3 4 4 5 5 5 6 7 7 7
the query should return 2.
I have no idea how to do this. I hope someone can help me!
Btw, my table contains a huge number of rows. So if there are several possibilities I would also be happy to know which solution is the most efficient.
Try this:
SELECT COUNT(ID) FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING COUNT(ID) > 2) p
select count(*) from
(select count(id) as cnt,id from mytable group by id) da
where da.cnt>2
The inner query will give you how many elements does each id have. And the outer query will filter this.
SELECT
COUNT(ids)
FROM
(SELECT
COUNT(ID)AS ids
FROM
mytable
GROUP BY
ID
HAVING
ids>2
)AS tbl1
Updated :
SELECT count(ID)
FROM (
SELECT ID FROM mytable
GROUP BY ID
HAVING count(ID) > 2
) p
should do what you need

how to count in rows in mysql database

I have a column that takes user names. How can I count the number of instances of a users name. For example I have 10 rows and in column username i want to count all names that show up multiple times. I would like to build a list of the top contributors to my database. So if username alex shows up 5 times and jeff shows up 3 and april shows up 2 times i will count this and from that I can build my list.
Try GROUP BY:
SELECT username, COUNT(*) AS user_count
FROM yourtable
GROUP BY username
ORDER BY user_count DESC
Try something like
SELECT USER_NAME, COUNT(USER_NAME) FROM YOUR_TABLE GROUP BY USER_NAME;
If you want to get a count of all the usernames then you just do the following SQL:
Select Count(*) from tablename
If you want to get just the count of unique usernames
Select Count(*) from tablename Group by username

mysql query to find sum of different names

I use similar queries (10) as following queries (modified) to find sum
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil', 'anil', 'gaikwad')
...and:
SELECT sum(amount) AS amount
FROM `students`
WHERE sex='M'
&& name in ('salil1', 'anil1', 'gaikwad1')
i want to make a single query of the above 10 queries. is it possible?
You can use UNION
SELECT 'subset1', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil', 'anil', 'gaikwad')
UNION
SELECT 'subset2', sum(amount) AS amount FROM students WHERE sex='M' and name in ('salil1', 'anil1', 'gaikwad1')
However, you probably query these sets of students for a reason, perhaps anil, salil and gaikwad are one group of students. If so, you should reflect this in the database structure, not in your code.
You could add a field 'SUbset' or 'Group' or whatever that is, to students table, so it looks like this:
name group_id
salil 1
anil 1
gaikwad 1
salil1 2
...
Then you can do
select group_id, sum(amount) from students group by group_id
Try something like this
SELECT sum(amount) AS amount
FROM students INNER JOIN
(SELECT 'salil%' Val UNION SELECT 'anil%' UNION SELECT 'gaikwad%') s ON students.NAME LIKE s.Val
WHERE sex='M'
This allows you to use the values in the second Table to join with LIKE.