mysql several column ranking per group - mysql

I have table data in this format
studno name level year term subject1 subject2 subject3
212 victor l1 2000 1 45 56 80
213 HOM l1 2000 1 42 56 70
214 ken l1 2000 1 60 70 50
215 ted l1 2000 1 46 36 47
212 victor l1 2000 2 45 36 68
213 Hom l1 2000 2 38 78 49
214 ken l1 2000 2 38 34 62
my desired output is the following
studno name level year term subject1 sub1rank subject2 sub2rank
213 victor l1 2000 1 42 3 56 2
214 HOM l1 2000 1 60 1 70 1
215 TED l1 2000 1 46 2 36 3
212 victor l1 2000 2 45 2 36 1
213 hOM l1 2000 2 38 3 36 1
214 KEN l1 2000 2 38 3 32 3
215 TED l1 2000 2 90 1 30 4
I have managed to get the rank but the problem is how to get the rank per year, level, term and subject. another problem is that if i use nested statement and try to create view in mysql database it throws an error, "View's SELECT contains a subquery in the FROM clause"

You can do this with correlated subqueries in the select clause. If I understand correctly, something like this:
select t.*,
(select COUNT(distinct t1.subject1)
from t t2
where t2.level = t.level and t2.year = t.year and t2.term = t.term and
t2.subject1 >= t.subject1
) as subj1rank,
(select COUNT(distinct t2.subject2)
from t t2
where t2.level = t.level and t2.year = t.year and t2.term = t.term and
t2.subject2 >= t.subject2
) as subj2rank
from t
The count(*) might be count(distinct subject1) (etc.), depending on how you treat ties.

Related

create a new column based on the conditions applied on the calculated average value in mysql, finally join the tables

I have two tables in mysql database
subjectids
id subject
11 Physics
12 Chemistry
13 Maths
14 Biology
15 History
16 Geography
studentsScores
id student subjectid score
1 Ahaan 11 45
2 Ahaan 12 33
3 Ahaan 13 49
4 Ivaan 11 41
5 Ivaan 12 38
6 Ivaan 13 46
7 Ann 11 40
8 Ann 12 30
9 Ann 13 50
I am trying to find the average of each subject and give a tag of easy , medium, hard based on the average value, like hard if avg<35, medium if avg between 35 and 45 and easy if avg greater than 45.
My expected result is
subject subjectid avg_score level
physics 11 42 medium
chemistry 12 33 hard
math 13 48 easy
I am new to sql, it would be great if you can help.
A simple JOIN and GROUP BY is enough to get your wanted result
SELECT `subject`, `subjectid`, ROUND(AVG(`score`),0) avg_score,
CASE
WHEN AVG(`score`) < 35 THEN 'hard'
WHEN AVG(`score`) between 35 and 45 then 'medium'
WHEN AVG(`score`) > 45 THEN 'easy'
end as level
FROM studentsScores ss JOIN subjectids si ON ss.`subjectid` = si.`id`
GROUP BY `subject`,`subjectid`
subject
subjectid
avg_score
level
Physics
11
42
medium
Chemistry
12
34
hard
Maths
13
48
easy
fiddle
A simple case statement would do the trick.
select si.subject,
si.id,
AVG(ss.score) as avg_score,
case when AVG(ss.score) < 35 then 'hard'
when AVG(ss.score) between 35 and 45 then 'medium'
when AVG(ss.score) > 45 then 'easy'
end as level
from subjectids si
inner join studentsScores ss on si.id=ss.subjectid
group by si.subject,si.id ;
https://dbfiddle.uk/IDS43R9W

Grouping records from LEFT JOIN in mysql if i already have ORDER BY statement and DISTINCT

I have 3 tables, and a query:
SELECT
DISTINCT assistent.id as id,
name,
events.client as client,
assistentprice.id as priceid,
value
FROM
`assistents`
LEFT JOIN `events` ON assistents.id = events.assistent
LEFT JOIN `assistentprice` ON assistents.id = assistentprice.id_assistente
ORDER BY
name
I got a result like:
id
name
client
priceid
value
88
MARK
44
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
27
11
10.00
88
MARK
44
10
9.00
16
OSCAR
49
21
8.00
16
OSCAR
14
23
9.00
16
OSCAR
14
22
7.00
16
OSCAR
49
19
9.00
So, table is ordered by name, but i want to see also ordered/grouped client for every assistent. For exampe, for Mark it have to be:
id
name
client
priceid
value
88
MARK
27
12
7.00
88
MARK
27
14
8.00
88
MARK
44
15
11.00
88
MARK
44
11
10.00
How can i do this?

select top 5 group by and order by

I have a table like this:
CITY QNT EXP RATE
LONDON 60 6 900
LONDON 35 8 337
LONDON 24 6 300
LONDON 22 6 266
BIRMINGHAM 22 6 266
NEWYORK 69 19 263
LONDON 21 6 250
ROME 24 7 242
BIRMINGHAM 24 7 242
BIRMINGHAM 24 7 242
LONDON 20 6 233
BIRMINGHAM 23 7 228
STUTTGART 29 9 222
LONDON 19 6 216
STUTTGART 25 8 212
PARIS 31 10 210
STUTTGART 34 11 209
STUTTGART 34 11 209
BIRMINGHAM 18 6 200
BIRMINGHAM 18 6 200
NEWYORK 18 6 200
BIRMINGHAM 17 6 183
LONDON 19 7 171
MUNICH 16 6 166
PARIS 21 8 162
STUTTGART 39 15 160
BARCELONA 18 7 157
LONDON 18 7 157
ROME 33 13 153
BARCELONA 15 6 150
PARIS 25 10 150
ROME 20 8 150
PARIS 25 10 150
ROME 20 8 150
LONDON 15 6 150
MUNICH 15 6 150
BIRMINGHAM 15 6 150
NEWYORK 15 6 150
LONDON 17 7 142
MUNICH 17 7 142
Here is my sql command:
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city
order by RATE desc
I want to group by city these results. But I couldn't do it. I want the top5 line to change most.
Result should be like that:
LONDON 60 6 900
BIRMINGHAM 22 6 266
NEWYORK 69 19 263
ROME 24 7 242
STUTTGART 29 9 222
This is from MySQL 5.6.
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city GROUP BY CITY
order by RATE desc LIMIT 5
Tested in this link: http://www.sqlfiddle.com/#!9/3f1ea1/1
This is from MS SQL Server 2017
select TOP 5 f.CITY, f.QNT, f.EXP, x.RATE
from (
select CITY, MAX((QNT-EXP)*100/EXP) as RATE
from tbl_city GROUP BY CITY
) as x inner join tbl_city as f on f.CITY = x.CITY
and ((f.QNT-f.EXP)*100/f.EXP) = x.RATE
ORDER BY RATE DESC;
Tested in this link: http://www.sqlfiddle.com/#!18/7b8da/61
Maybe you want something like this?
select top 5 CITY, QNT, EXP, RATE
from (
select *, row_number() over (partition by CITY order by RATE desc) AS RN
from (
select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city
) X
) Y
where RN = 1
order by RATE desc
I didn't test this, but it should take first the row for the city with biggest rate, and then take top 5 rows so that that the same city is not duplicated
I don't know what is logic behind the specified formula but this does what you want
select top 5 * from (
select top(1) with ties city, qnt, exp, (qnt-exp)*100/exp as rate
from tbl_city
order by row_number() over (partition by city order by (qnt-exp)*100/exp desc)
)t
order by rate desc
However, top(1) with ties with analytical functions are available in SQL Server.

Converting Rows in Datas to Columns for results obtained from multiple joins in Mysql

SELECT DataID.unique_id as unique_id, question.question_id
FROM DataID
RIGHT JOIN bookend
on DataID.user_id = bookend.user_id and DataID.client_id = bookend.client_id
LEFT JOIN question
on bookend.user_id = question.user_id
where type = 'test';
I am getting this value
unique_id question_id answer_id
2333 23 1
2333 24 5
2333 25 10
321 23 2
321 24 6
321 25 7
But i need to display it as
unique_id Q1 A1 Q2 A2 Q3 A3
2333 23 1 24 5 25 10
321 23 2 24 6 25 7
How Could i do this?

how do i join two different tables in mysql

This is table1:
id name m1 m2 m3 total itemno
1 raj 10 10 10 30 1
2 ram 60 60 60 180 1
3 kumar 70 70 70 210 1
4 kanna 50 50 50 150 1
5 vivek 64 64 91 200 1
5 vivek 90 90 90 270 2
This is table2:
id name mark1 mark2 mark3 itemno
101 vivek 78 78 78 1
102 vivekkanna 89 88 78 1
103 rajagopalan 97 90 98 1
104 kumar 69 54 56 1
101 vivek 90 90 90 2
I want to join these two tables like this into a result set that looks like this:
id name m1 m2 m3 total mark1 mark2 mark3 item no
1 raj 10 10 10 30 0 0 0 1
2 ram 60 60 60 180 0 0 0 1
3 kumar 70 70 70 210 69 54 56 1
4 kanna 50 50 50 150 0 0 0 1
5 vivek 64 64 91 200 78 78 78 1
5 vivek 90 90 90 270 90 90 90 2
Seems you want a regular LEFT JOIN, returning a default value if the row in table2 does not exist;
SELECT t1.id, t1.name, t1.m1, t1.m2, t1.m3,
COALESCE(t2.mark1, 0) mark1, COALESCE(t2.mark2, 0) mark2, t1.itemno
FROM table1 t1
LEFT JOIN table2 t2 ON t1.name = t2.name AND t1.itemno = t2.itemno
ORDER BY t1.id, t1.itemno
An SQLfiddle to test with
We use a LEFT JOIN to get the default NULL value for all table2 fields that don't match with a row in table1, then COALESCE to turn them into 0 instead.