I have basically following table:
id (int) username (string) messsage (string) rating (int)
So the entries look like this:
1 thomas "hello..." 3
2 Tina "blabla" 2
3 thomas "blub" 1
4 julia "basgs" 3
...
I want retrieve the top 10 usernames with the most ratings for all their messages.
So I want to sort the table that it looks
1. thomas 4
2. julia 3
3. Tina 2
I'm not sue I understood your question right, but try that one:
SELECT
username,
SUM(rating)
FROM
YourTable
GROUP BY
username
ORDER BY
SUM(rating) desc
LIMIT
10
select username
, count(*)
from YourTable
group by
username
order by
count(*) desc
limit 10
Related
I have a table name POEMS with columns poemID and userID. I want to make the poemID distinct and show only the count in the userID column. There are 3 poemID's with 1. I want to make it show poemID 1 and userID would be 2. poemID 2 and userID 3, for the 3 rows and poemID 3 with userID 1 for the 1 row.
|poemID | userID|
1 1
1 5
2 2
2 5
2 4
3 2
I want the above table to look like the table below.
|poemID | userID |
1 2
2 3
3 1
My SQL query im trying is below but its not working. Please help.
SELECT DISTINCT(poemID), COUNT(userID) FROM POEMS GROUP BY poemID;
This looks like a straight aggregation query:
SELECT poemID, COUNT(*) no_users
FROM POEMS
GROUP BY poemID;
Or, if the same user may appear multiple times for a given poem and you want to count it only once:
SELECT poemID, COUNT(DISTINCT userID) no_distinct_users
FROM POEMS
GROUP BY poemID;
I have read the different answers here on SO, but I am stuck on this question. Please help.
I have this mysql view named "activeuser":
userid COUNT(*) ACRONYM
1 23 admin
2 2 doe
3 4 tompa
12 4 Marre
13 1 Mia
1 2 admin
3 1 tompa
12 1 Marre
13 1 Mia
2 1 doe
3 1 tompa
12 1 Marre
How can I sum the COUNT column so that I get the following wanted result?
userid COUNT(*) ACRONYM
1 25 admin
2 3 doe
3 6 tompa
12 6 Marre
13 1 Mia
EDITED:
I used this query to create the view:
CREATE VIEW activeuser AS
(SELECT boats_comments.userid, COUNT(boats_comments.userid), boats_user.acronym, boats_user.email
FROM boats_comments
INNER JOIN boats_user
ON boats_comments.userid = boats_user.id
GROUP BY boats_comments.userid
ORDER BY COUNT(boats_comments.userid) DESC)
UNION ALL
(SELECT boats_answers.userid, COUNT(boats_answers.userid), boats_user.acronym, boats_user.email
FROM boats_answers
INNER JOIN boats_user
ON boats_answers.userid = boats_user.id
GROUP BY boats_answers.userid
ORDER BY COUNT(boats_answers.userid) DESC)
UNION ALL
(SELECT boats_questions.userid, COUNT(boats_questions.userid), boats_user.acronym, boats_user.email
FROM boats_questions
INNER JOIN boats_user
ON boats_questions.userid = boats_user.id
GROUP BY boats_questions.userid
ORDER BY COUNT(boats_questions.userid) DESC)
My goal is to see which users are the most active by checking the number of comments, questions and answers... but I got stuck...
As the results in your view has duplicates I guess the underlying code for the view is grouping on something it maybe shouldn't be grouping on.
You can get the results you want by applying SUM to it:
select userid, sum("whatever column2 is named") as "Count", Acronym
from activeuser group by userid, Acronym;
select userid, count(*) from activeuser group by userid;
Here's the table. It's ordered by points (desc) and id
id name points
1 ed 10
1 ed 9
2 jim 14
2 jim 8
2 jim 4
3 mike 11
Here's the results i'm looking for:
id name points
1 ed 10
2 jim 14
3 mike 11
How can this be done? basically, i want to list only the highest point row for each name and filter other rows away.
You can try something like this: use the MAX() function
SELECT id, name, MAX(points)
FROM your_table
GROUP BY id, name
ORDER BY points desc
Try this:
select id,name,max(points) from table1 group by id
So I have seen this done with multiple tables, but am confused as to how this would work with multiple tables. I want to select the number of correct entries in a tournament. This works except if a user got none right. Then it returns NULL. I want this to return 0.
Table-tournament_entries
Column
=============
id
tournament_id
game_id
user_id
username
prediction
correct
Here is the query that I run, I am hoping to return all users even the ones who did not get any questions right.
SELECT
tournament_id, username, user_id, COUNT(`prediction`)
FROM
tournament_entries
WHERE
correct = 1 AND tournament_id = 1
GROUP BY
username
ORDER BY
COUNT(`prediction`) DESC
LIMIT 0,10
EDIT: Sorry for all the confusion so when I run my query I get this
username user_id CorrectAns
mj 455 10
charlie 1 8
bill 2 8
doug 51 7
but there are users who are not show who have received 0 right. When I run the queries suggested I receive the number of questions .
username user_id CorrectAns
mj 455 16
charlie 1 16
bill 2 16
doug 51 16
sydney 452 16
Joe 218 16
If you notice sydney and joe are not in the first output but are in the second one
Use this query:
$query ="SELECT tournament_id, username, user_id, COALESCE(COUNT(`prediction`),0) as cpred
FROM tournament_entries
WHERE tournament_id = 1
GROUP BY username
ORDER BY cpred DESC
LIMIT 0,10"
COALESCE will return the first non-null parameter given to it.
Cheers.
Based on your comments to the other answer, I think what you're actually looking for is something like this (although it's hard to be sure without sample data and output):
SELECT
username, user_id, Sum(correct) As TotalCorrect
FROM
tournament_entries
WHERE
tournament_id = 1
GROUP BY
username, user_id
ORDER BY
TotalCorrect DESC
LIMIT 0,10
Name Score
Jim 1
Jim 2
Jim 4
Lisa 2
Lisa 5
Ted 1
Ted 2
Ted 3
How can i group by name, order by highest score, and only pick that one row? So The query would return 3 rows Jim 4, Lisa 5, and Ted 3.
To find the max score, you can GROUP BY name, and use the MAX function:
SELECT ns.Name, MAX(ns.Score) AS Score
FROM NameScore AS ns
GROUP BY ns.Name
ORDER BY ns.Name ASC
I made up the table name, since you did not provide one, switch that for your real table.
I think the following will work, but I haven't tested it:
SELECT Name, MAX(Score) FROM Table
GROUP BY Name
i think this is trust:
select from Table group by Name having MAX(Score);