How to find top 3 topper of each subject in given table - mysql

id - Name - Subject - Marks
1 - ABC - MAT - 90
2 - ABC - SCI - 80
3 - ABC - ENG - 90
4 - ABC - HIS - 96
5 - ABC - PHY - 70
6 - ABC - CHE - 43
7 - XYZ - MAT - 90
8 - XYZ - SCI - 80
9 - XYZ - ENG - 90
10 - XYZ - HIS - 96
11 - XYZ - PHY - 70
13 - XYZ - CHE - 43
etc .....
Just want to show 3 topper of each subject
ABC - MATH - 90
XYZ - MATH - 90
DEF - MATH - 80
etc

You can do this using variables.
select t.*
from (select t.*,
(#rn := if(#s = subject, #rn + 1,
if(#s := subject, 1, 1)
)
) as rn
from t cross join
(select #rn := 0, #s := '') params
order by subject, marks desc
) t
where rn <= 3
order by t.subject, t.rn;

With your data I create a SqlFiddleDemo.
I include two addtional student so query return only 3 from 4.
CREATE TABLE Courses
(`id` int, `Name` varchar(3), `Subject` varchar(3), `Marks` int);
And create this derivated to simplify the next code. The idea is create an unique code with 3 number 000-100 + Name so I can sorted during left outer join using AND L.comb <= R.comb.
Take note because this sorting in case of tie the latest name alphabetic will be showing first.
CREATE TABLE s_course AS
SELECT `id`, `Name`, `Subject`, `Marks`, concat(LPAD(`Marks`, 3, '0'), `Name`) as comb
FROM Courses;
Now the SELECT, if you run the inner select will see the 4 result and check how the ties are resolve.
SELECT *
FROM (
SELECT L.Subject, L.Marks, L.Name, count(*) as rn
FROM s_course L
left outer join s_course R
ON L.Subject = R.Subject
AND L.comb <= R.comb
GROUP BY L.Subject, L.comb
ORDER BY L.Subject, L.comb
) t
WHERE rn <= 3
ORDER BY Subject, rn
OR maybe you can ORDER BY Subject, Marks DESC, Name
This query exploit one issue of MySQL where you dont need put the same fields on select and group by
OUTPUT
| Subject | Marks | Name | rn |
|---------|-------|------|----|
| CHE | 48 | PQR | 1 |
| CHE | 48 | FGH | 2 |
| CHE | 43 | XYZ | 3 |
|---------|-------|------|----|
| ENG | 95 | PQR | 1 |
| ENG | 92 | FGH | 2 |
| ENG | 90 | XYZ | 3 |
|---------|-------|------|----|
| HIS | 96 | XYZ | 1 |
| HIS | 96 | ACB | 2 |
| HIS | 91 | PQR | 3 |
|---------|-------|------|----|
| MAT | 95 | PQR | 1 |
| MAT | 95 | FGH | 2 |
| MAT | 90 | XYZ | 3 |
|---------|-------|------|----|
| PHY | 75 | PQR | 1 |
| PHY | 70 | XYZ | 2 |
| PHY | 70 | ACB | 3 |
|---------|-------|------|----|
| SCI | 80 | XYZ | 1 |
| SCI | 80 | ACB | 2 |
| SCI | 75 | PQR | 3 |

select m1.id, max(m1.marks) 'marks', max(m1.subject) 'subject'
from marks m1
left join marks m2 on m2.subject =m1.subject and ( m2.marks>=m1.marks )
left join marks m3 on (m3.subject =m2.subject) and m3.id<>m1.id and m3.id<>m2.id and m3.marks>=m2.marks
left join marks m4 on (m4.subject =m3.subject) and m4.id<>m1.Id and m4.id<>m2.id and m4.id <>m3.id and m4.marks>=m3.marks
left join marks m12 on (m12.subject =m1.subject and m12.subject =m2.subject ) and m12.marks>m1.marks and m12.marks<m2.marks
left join marks m23 on (m23.subject=m2.subject and m23.subject=m3.subject) and ( m23.marks>m2.marks and m23.marks < m3.marks)
where
(m4.id is null)
and m12.id is null
and m23.id is null
group by m1.id
order by max(m1.subject) asc, max(m1.marks) desc

Related

MySQL - Select Top 5 with Rankings

I'm trying to get a users ranking getting his highest performances in every beatmap.
I get the user highest performance in every beatmap (only taking the top 5 performances) and adding them together, but it fails when the highest performance in one beatmap is repeated... because it counts twice
I'm based in this solution, but it doesn't works well for me...
Using MySQL 5.7
What i'm doing wrong?
Fiddle
Using this code:
SET group_concat_max_len := 1000000;
SELECT #i:=#i+1 rank, x.userID, x.totalperformance FROM (SELECT r.userID, SUM(r.performance) as totalperformance
FROM
(SELECT Rankings.*
FROM Rankings INNER JOIN (
SELECT userID, GROUP_CONCAT(performance ORDER BY performance DESC) grouped_performance
FROM Rankings
GROUP BY userID) group_max
ON Rankings.userID = group_max.userID
AND FIND_IN_SET(performance, grouped_performance) <= 5
ORDER BY
Rankings.userID, Rankings.performance DESC) as r
GROUP BY userID) x
JOIN
(SELECT #i:=0) vars
ORDER BY x.totalperformance DESC
Expected result:
+------+--------+------------------+
| rank | userID | totalperformance |
+------+--------+------------------+
| 1 | 1 | 450 |
+------+--------+------------------+
| 2 | 2 | 250 |
+------+--------+------------------+
| 3 | 5 | 140 |
+------+--------+------------------+
| 4 | 3 | 50 |
+------+--------+------------------+
| 5 | 75 | 10 |
+------+--------+------------------+
| 6 | 45 | 0 | --
+------+--------+------------------+
| 7 | 70 | 0 | ----> This order is not relevant
+------+--------+------------------+
| 8 | 76 | 0 | --
+------+--------+------------------+
Actual Result:
+------+--------+------------------+
| rank | userID | totalperformance |
+------+--------+------------------+
| 1 | 1 | 520 |
+------+--------+------------------+
| 2 | 2 | 350 |
+------+--------+------------------+
| 3 | 5 | 220 |
+------+--------+------------------+
| 4 | 3 | 100 |
+------+--------+------------------+
| 5 | 75 | 10 |
+------+--------+------------------+
| 6 | 45 | 0 | --
+------+--------+------------------+
| 7 | 70 | 0 | ----> This order is not relevant
+------+--------+------------------+
| 8 | 76 | 0 | --
+------+--------+------------------+
As you have mentioned that you are picking only top 5 performances per user across beatmaps then you can try this way:
select #i:=#i+1, userid,performance from (
select userid,sum(performance) as performance from (
select
#row_number := CASE WHEN #last_category <> t1.userID THEN 1 ELSE #row_number + 1 END AS row_number,
#last_category :=t1.userid,
t1.userid,
t1.beatmapid,
t1.performance
from (
select
userid, beatmapid,
max(performance) as performance
from Rankings
group by userid, beatmapid
) t1
CROSS JOIN (SELECT #row_number := 0, #last_category := null) t2
ORDER BY t1.userID , t1.performance desc
) t3
where row_number<=5
group by userid
)
t4 join (SELECT #i := 0 ) t5
order by performance desc
Above query will not consider duplicate Performance Score and pick only top 5 performance values.
DEMO

How to attribute dynamic values for table in mysql

I have 3 tables, 1 with prices, another with customers and the last with dependents of customers like a health insurance. When I have 1 customer and 1 dependent the value for the first dependent is one, when I have two dependents the value of second dependent is different, but the first is the same.
I need a query that shows the dependents table and gets the value of each dependent, 4 dependents are the maximum.
Dependent Table
+--------------+--------+---------+------------+
| id_dependent | name | number | primary_id |
+--------------+--------+---------+------------+
| 51 | Carlos | 956585 | 2 |
| 52 | João | 985868 | 2 |
| 53 | Jaime | 985868 | 2 |
| 54 | Evan | 985847 | 3 |
| 55 | Kaus | 584788 | 3 |
+--------------+--------+---------+------------+
Price Table
+----------+---------+-----------+-------+---------+
| price_id | Product | Dependent | Value | Plan_id |
+----------+---------+-----------+-------+---------+
| 11 | Plan1 | 1 | 15,00 | 56 |
| 12 | Plan1 | 2 | 13,50 | 56 |
| 13 | Plan1 | 3 | 11,50 | 56 |
+----------+---------+-----------+-------+---------+
What I need
+--------------+--------+--------+------------+-------+
| id_dependent | name | number | primary_id | Value |
+--------------+--------+--------+------------+-------+
| 51 | Carlos | 956585 | 2 | 15,00 |
| 52 | João | 985868 | 2 | 13,50 |
| 53 | Jaime | 985868 | 2 | 11,50 |
| 54 | Evan | 985847 | 3 | 15,00 |
| 55 | Kaus | 584788 | 3 | 13,50 |
+--------------+--------+--------+------------+-------+
How can I do this?
You can use row_number() to enumerate the dependents and then join:
select d.*, p.price
from (select d.*, row_number() over (partition by primary_id order by id_dependent) as seqnum
from dependents d
) d left join
price p
on p.dependent = d.seqnum and p.plan_id = 56;
In earlier versions of MySQL, you can use variables:
select d.*, p.price
from (select d.*,
(#rn := if(#p = d.primary_id, #rn + 1,
if(#p := d.primary_id, 1, 1)
)
) as seqnum
from (select d.* from dependents d order by primary_id, id_dependent) d cross join
(select #p := -1, #rn := 0) params
) d left join
price p
on p.dependent = d.seqnum and p.plan_id = 56;
Notes on the use of variables:
They are deprecated and may be removed in future versions of MySQL.
The order by is in a subquery; that is needed in some versions of MySQL. Variables and order by don't always play well together.
Both variables are assigned in the same expression. MySQL does not guarantee the order of evaluation of expressions, so this is very important for working code.
This works with mysql 5.6 and 5.7
Select d.`id_dependent`, d.`name`, d.`number`, d.`primary_id`, p.`Value`
From (
SELECT d.*,
if (#primid = primary_id,#curRank := #curRank + 1,#curRank := 1) AS rank,
#primid := primary_id
FROM Dependent d, (SELECT #curRank := 0) r , (SELECT #primid := 0) s
ORDER BY primary_id,id_dependent) d
left join price p on p.Dependent = d.rank
Order by d.`id_dependent`;
Which results in
id_dependent name number primary_id Value
51 Carlos 956585 2 15,00
52 João 985868 2 13,50
53 Jaime 985868 2 11,50
54 Evan 985847 3 15,00
55 Kaus 584788 3 13,50

Multi-event tournament standings (with arbitrary number of entries)

Suppose you have a a multi-event competition where competitors can attempt any event an arbitrary number of times. (weird, I know.)
How do pull out a desired player's best time for each event,
and assign it a placing? (1st 2nd 3rd...)
Data example: Desired output:
Name | Event | Score Name | Event | Score | Rank
-------------------- ----------------------------
Bob 1 50 Given input: "Bob"
Bob 1 100 Bob 1 100 1
Bob 2 75 Bob 2 75 3
Bob 3 80 Bob 3 80 2
Bob 3 65
Given input: "Jill"
Jill 2 75 Jill 2 90 1
Jill 2 90 Jill 3 60 3
Jill 3 60
Given input: "Chris"
Chris 1 70 Chris 1 70 2
Chris 2 50 Chris 2 85 2
Chris 2 85 Chris 3 100 1
Chris 3 100
This is a build up of my previous question:
Multi-event tournament standings
I feel understand that problem much better (Thanks!), but I cannot bridge the gap to this version of the problem.
I have SQL 5.x so I cant use stuff like Rank(). This will also be crunching many thousands of scores.
Desired output can be acheaved with this query:
select
IF(event is NULL, CONCAT('Given input: "', name,'"'), name) as name,
IF(event is NULL, '', event) as event,
IF(event is NULL, '', max(score)) as score,
IF(event is NULL, '', (
select count(s2.name) + 1
from (
select name, max(score) as score
from scores es
where es.event = s.event
group by es.name
order by score desc
) s2
where s2.score > max(s.score)
)) as `rank`
from scores s
group by name, event with rollup
having name is not NULL
order by name, event;
And output (if run query in mysql cli):
+----------------------+-------+-------+------+
| name | event | score | rank |
+----------------------+-------+-------+------+
| Given input: "Bob" | | | |
| Bob | 1 | 100 | 1 |
| Bob | 2 | 75 | 3 |
| Bob | 3 | 80 | 2 |
| Given input: "Chris" | | | |
| Chris | 1 | 70 | 2 |
| Chris | 2 | 85 | 2 |
| Chris | 3 | 100 | 1 |
| Given input: "Jill" | | | |
| Jill | 2 | 90 | 1 |
| Jill | 3 | 60 | 3 |
+----------------------+-------+-------+------+
11 rows in set, 3 warnings (0.00 sec)
Should work on any Mysql 5.
You can get the highest score per event by an aggregation by event taking the max(). To simulate a dense_rank() you can use a subquery counting the scores higher than or equal to the current score per event.
For a particular contestant (here Bob) that makes:
SELECT d1.name,
d1.event,
max(d1.score) score,
(SELECT count(*)
FROM (SELECT d2.event,
max(d2.score) score
FROM data d2
GROUP BY d2.event,
d2.name) x1
WHERE x1.score >= max(d1.score)
AND x1.event = d1.event) rank
FROM data d1
WHERE d1.name = 'Bob'
GROUP BY d1.event
ORDER BY d1.event;
And for all of them at once:
SELECT d1.name,
d1.event,
max(d1.score) score,
(SELECT count(*)
FROM (SELECT d2.event,
max(d2.score) score
FROM data d2
GROUP BY d2.event,
d2.name) x1
WHERE x1.score >= max(d1.score)
AND x1.event = d1.event) rank
FROM data d1
GROUP BY d1.name,
d1.event
ORDER BY d1.name,
d1.event;
db<>fiddle
E.g.:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,name VARCHAR(12) NOT NULL
,event INT NOT NULL
,score INT NOT NULL
);
INSERT INTO my_table (name,event,score) VALUES
('Bob' ,1, 50),
('Bob' ,1,100),
('Bob' ,2, 75),
('Bob' ,3, 80),
('Bob' ,3, 65),
('Jill' ,2, 75),
('Jill' ,2, 90),
('Jill' ,3, 60),
('Chris',1, 70),
('Chris',2, 50),
('Chris',2, 85),
('Chris',3,100);
SELECT a.*
, FIND_IN_SET(a.score,b.scores) my_rank
FROM my_table a -- it's possible that this really needs to be a repeat of the subquery below, so
-- ( SELECT m.* FROM my_table m JOIN (SELECT name,event,MAX(score) score FROM my_table
-- GROUP BY name, event) n ON n.name = m.name AND n.event = m.event AND n.score = m.score) AS a
JOIN
(
SELECT x.event
, GROUP_CONCAT(DISTINCT x.score ORDER BY x.score DESC) scores
FROM my_table x
JOIN
( SELECT name
, event
, MAX(score) score
FROM my_table
GROUP
BY name
, event
) y
ON y.name = x.name
AND y.event = x.event
AND y.score = x.score
GROUP
BY x.event
) b
ON b.event = a.event
WHERE FIND_IN_SET(a.score,b.scores) >0;
+----+-------+-------+-------+------+
| id | name | event | score | rank |
+----+-------+-------+-------+------+
| 2 | Bob | 1 | 100 | 1 |
| 3 | Bob | 2 | 75 | 3 |
| 4 | Bob | 3 | 80 | 2 |
| 6 | Jill | 2 | 75 | 3 |
| 7 | Jill | 2 | 90 | 1 |
| 8 | Jill | 3 | 60 | 3 |
| 9 | Chris | 1 | 70 | 2 |
| 11 | Chris | 2 | 85 | 2 |
| 12 | Chris | 3 | 100 | 1 |
+----+-------+-------+-------+------+

Latest datetime from unique mysql index

I have a table. It has a pk of id and an index of [service, check, datetime].
id service check datetime score
---|-------|-------|----------|-----
1 | 1 | 4 |4/03/2009 | 399
2 | 2 | 4 |4/03/2009 | 522
3 | 1 | 5 |4/03/2009 | 244
4 | 2 | 5 |4/03/2009 | 555
5 | 1 | 4 |4/04/2009 | 111
6 | 2 | 4 |4/04/2009 | 322
7 | 1 | 5 |4/05/2009 | 455
8 | 2 | 5 |4/05/2009 | 675
Given a service 2 I need to select the rows for each unique check where it has the max date. So my result would look like this table.
id service check datetime score
---|-------|-------|----------|-----
6 | 2 | 4 |4/04/2009 | 322
8 | 2 | 5 |4/05/2009 | 675
Is there a short query for this? The best I have is this, but it returns too many checks. I just need the unique checks at it's latest datetime.
SELECT * FROM table where service=?;
First you need find out the biggest date for each check
SELECT `check`, MAX(`datetime`)
FROM YourTable
WHERE `service` = 2
GROUP BY `check`
Then join back to get the rest of the data.
SELECT Y.*
FROM YourTable Y
JOIN ( SELECT `check`, MAX(`datetime`) as m_date
FROM YourTable
WHERE `service` = 2
GROUP BY check) as `filter`
ON Y.`service` = `filter`.service
AND Y.`datetime` = `fiter`.m_date
WHERE Y.`service` = 2

mysql select top N records conditionally

i have the following table :
+------+------+-----+---------+------------+
| no | code |eot |group_id | compulsary |
+------+------+-----+---------+------------+
| 1005 | 101 | 51 | 1 | 1 |
| 1005 | 102 | 67 | 1 | 1 |
| 1005 | 121 | 65 | 1 | 1 |
| 1005 | 231 | 82 | 2 | 0 |
| 1005 | 232 | 56 | 2 | 0 |
| 1005 | 233 | 45 | 2 | 1 |
| 1005 | 313 | 80 | 3 | 0 |
| 1005 | 443 | 50 | 4 | 0 |
|------+------+-----+---------+------------+
now what i want is :
1.) return all records where group_id=1,
2.) return the best two from group_id=2(however if compulsary=1 then include that row and the best from the remaining group_id=2),
3.)return 1 row each from group_id=3 and group_id=4 and if compulsary=1 then return that row
the final result should have only seven rows:
+------+------+-----+---------+------------+
| no | code |eot |group_id | compulsary |
+------+------+-----+---------+------------+
| 1005 | 101 | 51 | 1 | 1 |
| 1005 | 102 | 67 | 1 | 1 |
| 1005 | 121 | 65 | 1 | 1 |
| 1005 | 231 | 82 | 2 | 0 |
| 1005 | 233 | 45 | 2 | 1 |
| 1005 | 313 | 80 | 3 | 0 |
| 1005 | 443 | 50 | 4 | 0 |
|------+------+-----+---------+------------+
with the compulsary=1 rows inluded like above;
so far I have this query though I don't know how to check for the compulsary to get what I want:
select rg.*
from
(
select *
from
(
select
rgrade.*,
#rn := if(#gr=group_id,if(#gr:=group_id,#rn+1,#rn+1),if(#gr:=group_id,1,1)) as rn
from rgrade
cross join (select #rn:=0,#gr:=0) as vars
where admission_no=1005
) v
where (group_id=1)
or (group_id=2 and if(compulsary=1,rn<=1,rn<=2))
or (group_id in (3,4) and rn=1)
) rg
order by group_id;
the query returns the seven rows as expected but does not check for compulsary in group_id=2.
Any help much appreciated
You are describing several queries here, the result of which you want combined: All group_id = 1, the two best group_id = 2, the best of group_id = 3, and the best of group_id = 4. So write these queries and combine them with UNION ALL. With "best" defined as compulsary = 1 preferred, then highest eot, you get:
(select * from mytable where group_id = 1)
union all
(select * from mytable where group_id = 2 order by compulsary = 1 desc, eot desc limit 2)
union all
(select * from mytable where group_id = 3 order by compulsary = 1 desc, eot desc limit 1)
union all
(select * from mytable where group_id = 4 order by compulsary = 1 desc, eot desc limit 1)
order by group_id, no, code
;
You are trying to mimic standard SQL's
row_number() over (partition by group_id
order by case when compulsary = 1 then 1 else 0 end desc, eot desc)
with MySQL means. And I see the partitioning by group_id in your query, but I don't see any ORDER BY to get the best records first.
Here is my attempt on it. There may be mistakes; I'm no MySQL guy.
select
no, code, eot, group_id, compulsary
from
(
select
no,
code,
eot,
compulsary,
#row_number :=
case when group_id = #last_group_id then #row_number + 1 else 1 end as row_number,
#last_group_id := group_id as group_id
from rgrade
cross join
(
select
#row_number := 0,
#last_group_id := -1
) as vars
where admission_no = 1005
order by group_id, (compulsary = 1) desc, eot desc
) ranked
where (group_id = 1)
or (group_id = 2 and row_number <= 2)
or (group_id = 3 and row_number = 1)
or (group_id = 4 and row_number = 1)
order by group_id, code;