I have four columns output in my resultset:
Name, Subject, Grade, Value
Is it possible to populate a result set using the Max(Value), but also bring through it's adjacent Subject and Grade and how would I achieve this? eg:
Name Subject Grade Value
Helen Chemistry C 7
Helen Physics B 8
Helen Biology A 9
Brad Chemistry C 7
Brad Biology D 6
Brad Physics F 4
Becomes
Name Subject Grade Value
Helen Biology A 9
Brad Chemistry C 7
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY value DESC) rn
FROM mytable
) q
WHERE rn = 1
Related
The table f has these values:
name1 name2 count
Harrison Ford Mullah Omar 3
Harrison Ford Michael Caine 6
Michael Caine Mullah Omar 2
Michael Caine Harrison Ford 6
Mullah Omar Michael Caine 2
Mullah Omar Harrison Ford 3
How to choose only the distinct pairs?
select
distinct name1,
distinct name2, count from f group by name1
Given that you have a duplicate for each pair, you can just use:
select f.*
from f
where name1 < name2;
If you did not have the reverse in all cases, you can use:
select f.*
from f
where f.name1 < f.name2 or
not exists (select 1
from f f2
where f2.name1 = f.name2 and f2.name2 = f.name1
);
id subject points Rank
joe maths 70 1
Mike maths 60 2
Sarah maths 40 3
mike English 80 1
Sarah English 65 2
joe English 55 3
Sarah Chemistry 80 1
Mike Chemistry 60 2
joe Chemistry 43 3
I was able to query this in mysql but i want to store the rank column in the table (grades) using the ALTER command
i tried
ALTER TABLE grades
ADD COLUMN Rank
GENERATE ALWAYS AS
DENSE_RANK() OVER(PARTITION BY subjects ORDER BY points DESC)
ORDER BY id,
Rank
but did not work
Hope i could find some help out there
Below are the given tables:
`student` `subject`
------------------- -------------------
id name id subject
------------------- -------------------
1 Alice 1 Maths
2 Bob 2 Science
3 Eve 3 Economics
------------------- -------------------
`marks`
-----------------------------------------------------
id student_id subject_id marks
-----------------------------------------------------
1 1 1 30
2 1 2 40
3 2 3 50
4 3 1 60
5 3 2 70
-----------------------------------------------------
I need an output which should look like below:
`o/p`
----------------------------------------------
name subject marks
----------------------------------------------
Alice Maths 30
Alice Science 10
Alice Economics NULL
Bob Maths NULL
Bob Science NULL
Bob Economics 50
Eve Maths 60
Eve Science 70
Eve Economics NULL
----------------------------------------------
Please note that I am targeting MySQL 5.6.x syntax and I have created a SQL fiddle of above here to ease access to the question.
Here you go
select
st.name,
su.subject,
m.marks
from student as st
cross join subject as su
left join marks as m on m.student_id=st.id and m.subject_id=su.id
order by st.name, su.subject
SELECT student.name,
subject.subject,
marks.marks
FROM student
JOIN subject ON student.id = subject.id
JOIN marks ON student.id = marks.id
ORDER BY student.name,
subject.subject
Can someone please help me to 'ORDER' a mysql table according to the no. of occurrences of a string in one of the columns of same table.
I want to rearrange the table according to the number of times a location has occurred.
Please see the example below:
id name location
-- ----- --------
1 Mark US
2 Mike US
3 Paul Australia
4 Pranshu India
5 Pranav India
6 John Canada
7 Rishab India
Expected result:
id name location
-- ----- --------
4 Pranshu India
5 Pranav India
7 Rishab India
1 Mark US
2 Mike US
3 Paul Australia
6 John Canada
I tried this query:
SELECT *, COUNT(location) AS count FROM `tablename` GROUP BY `location` ORDER BY count DESC;
But it showed me the following result omitting the repeating occurrence of location:
id name location
-- ----- --------
4 Pranshu India
1 Mark US
3 Paul Australia
6 John Canada
SELECT x.*
FROM my_table x
JOIN (SELECT location, COUNT(*) total FROM my_table GROUP BY location) y
ON y.location = x.location
ORDER
BY total DESC
, id;
How do you order a mysql table by the number of occurrences in a spesific column?
Example table:
ID Name
1 Alfred
2 Alfred
3 Burt
4 Alfred
5 Jill
6 Jill
7 Jill
8 Jill
9 Burt
The sorted table should be like below, since "Jill" is the name occurring most, it should be sorted first, and so on:
ID Name
5 Jill
6 Jill
7 Jill
8 Jill
1 Alfred
2 Alfred
4 Alfred
3 Burt
9 Burt
You have to bring in the information into the query. This is typically done using a join:
select e.*
from example e join
(select name, count(*) as cnt
from example
group by name
) en
on e.name = en.name
order by cnt desc, e.name, e.id;
Note that the order by not only orders by the count. It also orders by the name. If two names have the same count, then it will keep them together.