I have 2 tables "quizzes" & "users", I need to return list of first user took each quiz, with quiz_id:
tables structure
"quizzes" structure:
id name
1 England
2 france
3 Japan
4 USA
5 UAE
6 Sweden
7 Italy
8 Brazil
9 South Korea
10 India
"users" structure:
id user_id quiz_id
1 1 1
2 1 2
3 2 1
4 3 4
5 1 4
6 5 9
7 2 9
8 3 8
9 3 9
10 3 7
I need to run query to return first "user_id" took each "quiz", (order by users.id ASC)
expected results:
quiz_id user_id
1 1
2 1
4 3
7 3
8 3
9 5
thanks,
You first group by quiz and pick minimal id and then select based on those ids:
select quiz_id, user_id
from users
where id in(select min(id) from users group by quiz_id)
Select quiz_id, user_id
from users
Where (quiz_id, id) in
(
Select quiz_id, min(id) id
From users
Group by quiz_id
)
Related
i have table structure like this
id
user_id
parent_id
club
1
1
club1
2
2
1
club1
3
3
1
club1
4
4
2
club1
5
5
2
club1
6
6
3
club1
7
7
3
club1
8
8
4
club1
9
9
4
club1
10
10
5
club1
11
11
5
club1
12
12
6
club1
13
13
6
club1
14
14
7
club1
15
15
7
club1
i want to select user_id whose child is less then 2.
refer to above table user 1,2,3,4,5,6 and 7 two child complete so the query should return 8,9,10,11,12,13,14,15
Refer to above table here user_id 1 have two child 2 and 3 , user_id 2 have child 4 and 5 and so on.
i need to select user_id whose child is less than 2
You can do it as follows :
SELECT user_id
FROM club_tree
WHERE user_id NOT IN (
SELECT parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
)
and club = 'club1';
We Select users that are not in the list of users with more than two childs.
This is to get users with more than two childs :
SELECT parent_id
from club_tree
group by parent_id
HAVING count(1) >= 2
Here you can test it : http://sqlfiddle.com/#!9/eb2c70/3
Can you try this query using left join :
SELECT c.user_id
from club_tree c
left join
(
select parent_id
from club_tree
where club = 'club1'
group by parent_id
HAVING count(1) >= 2
) as c2 on c.user_id = c2.parent_id
where c.club = 'club1' and c2.parent_id is null;
Try it here : http://sqlfiddle.com/#!9/eb2c70/17
Table1:
***Publisher*** with following columns:
o **Publisher_id** ; unique ID for each publisher
o **Video_id** ; unique ID for each video
o **Video_duration**; in minutes for each video
Table2:
***Consumer*** with the following columns:
o **Video_id** ; unique ID for each video
o **User_id** ; unique ID for each customer
o **Dateid** ; the date on which the consumer watched a video
Example:
Publisher_id Video_id Video_duration
1 1 2
1 2 3
1 3 2
1 4 5
2 6 2
2 7 2
2 8 20
3 10 2
3 11 3
3 12 3
1 23 100
5 9 8
Video_id User_id Date_id
1 44 20210615
2 44 20210615
11 33 20210614
6 44 20210612
I think I mostly need to figure out how to sum up the time spent watching videos per user_id and then to corroborate that using a limit 5 select, but I can't figure how to combine these 2 selects
I'd try like this:
SELECT SUM(Video_duration)
FROM Consumer
INNER JOIN Publisher USING (Video_id)
GROUP BY User_id
ORDER BY SUM(Video_duration) DESC
LIMIT 5
I have a table of matches of a game in a tournement.
Here is the mysql table:
id
player1
player1_score
player1_rating
player2
player2_score
player2_rating
tourney_id
round
1
1
9
1.05
2
3
5.34
5
1
2
3
4
5.21
4
9
3.34
5
1
3
5
9
3.52
6
2
5.24
5
1
4
1
9
6.23
3
0
4.74
5
2
5
2
8
9.43
4
9
1.23
5
2
6
3
9
3.41
5
7
6.23
5
2
7
1
9
5.22
4
2
2.43
5
3
8
2
3
4.21
3
9
5.22
5
3
9
5
1
7.31
6
9
3.43
5
3
How can I get the average player ratings for each individual player in a particular tourney?
Please note, there's two columns with player ids (player1, player2)
Based on your requirements (Even though you do not give us your expected results), I assume your code should be like this:
SELECT (SELECT SUM(player1_rating) / COUNT(DISTINCT player1)
FROM your_table GROUP BY player1) as avg_player1,
(SELECT SUM(player2_rating) / COUNT(DISTINCT player2) FROM your_table
GROUP BY player2) as avg_player2
FROM your_table
I revised the query to:
SELECT player_id
, tourney_id
, avg_player_rating = SUM(player_rating) / SUM(cnt)
FROM (
SELECT player_id = player1
, tourney_id
, player_rating = SUM(player1_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player1
UNION ALL
SELECT player_id = player2
, tourney_id
, player_rating = SUM(player2_rating)
, cnt = COUNT(*)
FROM tournament
GROUP BY tourney_id, player2
) AS a
GROUP BY player_id, tourney_id
and this will give the result:
player_id tourney_id avg_player_rating
---------- ---------- --------------------
1 5 4.1666
2 5 6.3266
3 5 4.6450
4 5 2.3333
5 5 5.6866
6 5 4.3350
I hope that this is the result that you are looking for.
I have two tables, one of a list of competition results, and one with ELO ratings (based on previous competitions).
Fetching the list of competitors for an arbitrary competition is trivial enough, but I also need to get the most recent rating value for them.
score:
id | eventid | competitorid | position
1 1 1 1
2 1 2 2
3 1 3 3
4 2 2 1
5 2 3 2
6 3 1 1
7 3 3 2
8 3 2 3
rating:
id | competitorid | rating
1 1 1600
2 2 1500
3 3 1500
4 2 1600
5 3 1590
Expected output for a query against score.eventid = 3 would be
id | competitorid | position | rating
6 1 1 1600
7 3 2 1590
8 2 3 1600
At the moment my code looks like:
SELECT score.scoreID, score.competitorID, score.position,
rating.id, rating.rating
FROM score, rating
WHERE score.competitorid = rating.competitorid
AND score.eventid = 3
ORDER BY score.position
which gives an output of
id | competitorid | position | rating.id | rating
6 1 1 1 1600
7 3 2 2 1500
7 3 2 4 1590
8 2 3 3 1500
8 2 3 5 1600
basically it's showing the data from the score table for that correct event, but giving me a row for every rating available against that competitorID unfortunately I have no idea where to build in the DISTINCT statement or how to limit it to the most recent result.
MySQL noob, and managed DISTINCT statements, but not with joins. Unfortunately most previous questions seemed to deal with getting distinct results from a single table, which is not quite what I'm after. Thanks!
One way to get the rating is with a correlated subquery:
SELECT s.scoreID, s.eventID, s.competitorID, s.position,
(select r.rating
from rating r
where s.competitorID = r.competitorID
order by r.id desc
limit 1
) as rating
FROM score s
WHERE s.eventID = 3
ORDER BY s.position;
I'm not sure what ratingprid is, so this only includes the rating.
upload table
id category_id
1 1
2 2
3 3
4 1
In Ratings Table
id upload_id points
1 1 5
2 2 3
3 3 2
4 4 2
5 1 5
I want to display all the records from ratings table except id 4 because id 4 is the same category id 1
I excepted result
Uploaded_id 1 has sum of 10 points
Uploaded_id 2 has sum of 3 points
Uploaded_id 3 has sum of 2 points
Please help me.
Thanks In advance
Sasikumar
select upload_id, sum(points)
from rating r,
(select min(id) id from upload group by category_id) a
where a.id = r.upload_id group by upload_id