MySQL Query for Average Grade of last 2 attempts - mysql

I have a table:
quiz userid attempt grade
1 3 1 33
1 3 2 67
1 3 3 90
1 3 4 20
Now, I want the last two attempts i.e., 4 and 3 and I want average grade of these 2 grades i.e, 90 and 20
Could anyone help me?

Use ORDER and LIMIT to get the 2 last attempts and the AVG aggregation function :
SELECT AVG(grade) AS average FROM (
SELECT grade FROM table
WHERE userid = 3
ORDER BY attempt DESC LIMIT 2) AS t

If you want to list both test results separately, with the average in each row, then something like this maybe (otherwise you just need the subquery for the average of the two tests):
SELECT userid, attempt, grade,
( SELECT AVG(grade)
FROM table
ORDER BY attempt DESC LIMIT 0, 2 ) AS avg_grade
FROM table
ORDER BY attempt DESC LIMIT 0, 2;

Related

need help to find number of times a number is repeated in query result

I have a table like following
item_id link_id
1 10
1 20
2 100
2 40
2 10
3 10
3 30
4 10
4 20
4 30
I ran the query to find occurrence of each item_id
select `item_id`, count(`item_id`)
from `table`
group by `order_id`
which gave me the result
item_id count('item_id')
1 2
2 3
3 2
4 3
But I have to find out how many time did i had each value in result, something like this
count('item_id') Occurence
2 2
3 2
How should I update the query
Use two levels of aggregation:
select cnt, count(*), min(item_id), max(item_id)
from (select `item_id`, count(`item_id`) as cnt
from `table`
group by `order_id`
) i
group by cnt;
I also often add min(item_id), max(item_id) to such queries to get examples for each count.

sum id in every group taken one id

i got stuck for this query..
i just want to sum result of every id, but every id have many rows..
ID SUB Marks
1 English 25
2 English 25
3 English 25
4 Maths 10
5 Maths 10
6 Maths 10
i need the result like this
ID Marks
1 35
i just take one id every group, then i sum.. how do id finished it?
i try SUM( distinct..) but wrong result..
really need your helps guys..
select sum(marks)
from
(
select distinct sub, marks
from your_table
) tmp

Aggregation and finding mode of data set

I am aggregating data and I cannot sum certain columns so I would like to take the most frequent observation from that column, or the mode value. Each ID can have only one site and number, so if there are ties then pick the smaller of the two numbers.
Example follows:
ID site number
1 3 45
1 3 45
1 2 56
1 3 56
2 4 5
2 5 5
2 5 3
2 5 5
I want it to look like:
ID site number
1 3 45
2 5 5
Here's one way of doing it:
with aggregation as
(
select id
, site
, number
, numberCount = count(1)
from SiteNumbers
group by id
, site
, number
), aggregateRanks as
(
select *
, idRank = row_number() over (partition by id order by numberCount desc, number, site)
from aggregation
)
select id
, site
, number
from aggregateRanks
where idRank = 1
SQL Fiddle with demo.
It matches your results, but depending on all your different cases might need some tweaking; hopefully it gives you some ideas.

Select User Having Maximum Rating

I have a table which stores the rating given by the user.
eg:
UserId Rating
3 1
3 2
1 1
1 2
1 3
1 4
2 1
2 23
2 4
I need to retrieve the 10 users who have rated the maximum number of times.
eg:
1 rated 4 times
2 rated 3 times
3 rated 2 times...
Any idea how to write a query using mysql?
You have to group by userID, count the grouped rows, and order by the count in descending order, then you limit the query to 10 rows:
SELECT userID, count(*) times
FROM users
GROUP BY userID
ORDER BY times DESC
LIMIT 10
If you need the output exactly as shown above, try this:
SELECT CONCAT_WS(' ', userID, 'rated', count(*), 'times')
FROM users
GROUP BY userID
ORDER BY count(*) DESC
LIMIT 10
See this fiddle.

Access Totals Query Not Necessarily Returning First Record

I have a table of data like this:
id user_id A B C
=====================
1 15 1 2 3
2 15 1 2 5
3 20 1 3 9
4 20 1 3 7
I need to remove duplicate user ids and keep the record that sorts lowest when sorting by A then B then C. So using the above table, I set up a temp query (qry_temp) that simply does the sort--first on user_id, then on A, then on B, then on C. It returns the following:
id user_id A B C
====================
1 15 1 2 3
2 15 1 2 5
4 20 1 3 7
3 20 1 3 9
Then I wrote a Totals Query based on qry_temp that just had user_id (Group By) and then id (First), and I assumed this would return the following:
user_id id
===========
15 1
20 4
But it doesn't seem to do that--instead it appears to be just returning the lowest id in a group of duplicate user ids (so I get 1 and 3 instead of 1 and 4). Shouldn't the Totals query use the order of the query it's based upon? Is there a property setting in the query that might impact this or another way to get what I need? If it helps, here is the SQL:
SELECT qry_temp.user_id, First(qry_temp.ID) AS FirstOfID
FROM qry_temp
GROUP BY qry_temp.user_id;
You need a different type of query, for example:
SELECT tmp.id,
tmp.user_id,
tmp.a,
tmp.b,
tmp.c
FROM tmp
WHERE (( ( tmp.id ) IN (SELECT TOP 1 id
FROM tmp t
WHERE t.user_id = tmp.user_id
ORDER BY t.a,
t.b,
t.c,
t.id) ));
Where tmp is the name of your table. First, Last, Min and Max are not dependent on a sort order. In relational databases, sort orders are quite ephemeral.