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`
Related
Using PHP and MySQL, I want to query a table of postings my users have made to find the person who has posted the most entries.
What would be the correct query for this?
Sample table structure:
[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack
I would like to see that "johnnietheblack" is the top poster, "marywhite" is second to best, and "dannyrottenegg" has the least
Something like:
SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1
This gets the number of rows posted by a particular ID, then sorts though the count to find the highest value, outputting it, and the ID of the person. You'll need to replace the 'UserID' and 'postings' with the appropriate column and field though.
I believe this should work...
SELECT user_id, COUNT(*) FROM postings ORDER BY COUNT(*) GROUP BY user_id LIMIT 1
Assuming posting is a tuple (user_id, recipient_user_id), where each row represents one posting, from user_id to recipient_user_id:
select user_id, count(*) as posts
from postings
group by user_id
having count(*) = max(count(*)) ;
This question already has answers here:
How to count occurrences of a column value efficiently in SQL?
(7 answers)
Closed 5 years ago.
I have got a table with ID's:
Id
===
1
2
2
3
1
2
And I want to create a table with two columns like this:
Id COUNT
=============
1 2
2 3
3 1
How can I do that?
Let's say you called your table 'user', you can try this :
SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;
Hope it helps,
WaLinke
You have to group by your id.
Select id, count(1) as COUNT
from yourtable
group by ID
order by id
That way you tell your sql, that you want to count the number of rows per id.
If you need more examples feel free to google sql count.
There are many good examples out there.
Or check this stackoverflow question:
How to use count and group by at the same select statement
You can use GROUP BY and Count(columnname) functions like this
SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id
Something like this should work
SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id
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 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
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