I have a problem with MySQL to find student rank from average, my ranks are the student number, so if the the average number of the student equal, they will have the same number in rank.
Table point
id | using_student_id | using_subject_id | total_point |
1 | 1 | 1 | 59 |
2 | 1 | 2 | 53 |
3 | 2 | 1 | 90 |
4 | 2 | 2 | 29 |
5 | 3 | 1 | 90 |
6 | 3 | 2 | 29 |
--------------------------------------------------------
Table student
----------------------------------------------------
id | name | id | sex | dob | class |
1 | sovann | G-000 001 | M | 1993-03-04 | 1 |
2 | rady ny| G-000 002 | M | 1992-01-05 | 1 |
3 | dy sove| G-000 003 | M | 1991-07-03 | 1 |
----------------------------------------------------
Table subject
---------------------
id | name | class |
---------------------
1 | math | 1 |
2 | english| 1 |
---------------------
My desired result :
id | name |total| avg | rank |
1 | sovann | 112 | 56 | 1 |
2 | rady ny| 119 | 59.5 | 2 |
3 | dy sove| 119 | 59.5 | 2 |
---------------------------------
This is the query I tried :
SELECT s.name as name, sum(p.total_point) as total, avg(p.total_point) as average,
CASE
WHEN @prevRank = avg(p.total_point) THEN @curRank
WHEN @prevRank := avg(p.total_point) THEN @curRank := @curRank + 1
END AS rank
from tbl_point as p
inner join tbl_student s
on p.uing_student_id = s.id
inner join tbl_subject as su
on su.id = p.using_subject_id,
(SELECT @curRank :=0, @prevRank := NULL) r
group by s.id
result :
------------------------------------
name | total | average | rank | | rank
sovann | 112 | 56 | 1 | --> result should be| 2
rady ny| 119 | 59.5 | 2 | | 1
dy sove| 119 | 59.5 | 3 | | 1
------------------------------------
this my table :
-- Dumping structure for table student_db.tbl_class
CREATE TABLE IF NOT EXISTS `tbl_class` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- Dumping data for table student_db.tbl_class: ~3 rows (approximately)
/*!40000 ALTER TABLE `tbl_class` DISABLE KEYS */;
REPLACE INTO `tbl_class` (`id`, `name`) VALUES
(1, 'Class_one'),
(2, 'Class_two'),
(3, 'Class_three');
/*!40000 ALTER TABLE `tbl_class` ENABLE KEYS */;
-- Dumping structure for table student_db.tbl_point
CREATE TABLE IF NOT EXISTS `tbl_point` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`total_point` float DEFAULT NULL,
`uing_student_id` int(11) NOT NULL,
`using_subject_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_point_tbl_student_idx` (`uing_student_id`),
KEY `fk_tbl_point_tbl_subject1_idx` (`using_subject_id`),
CONSTRAINT `fk_tbl_point_tbl_student` FOREIGN KEY (`uing_student_id`) REFERENCES `tbl_student` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_tbl_point_tbl_subject1` FOREIGN KEY (`using_subject_id`) REFERENCES `tbl_subject` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- Dumping data for table student_db.tbl_point: ~0 rows (approximately)
/*!40000 ALTER TABLE `tbl_point` DISABLE KEYS */;
REPLACE INTO `tbl_point` (`id`, `total_point`, `uing_student_id`, `using_subject_id`) VALUES
(1, 59, 1, 1),
(2, 53, 1, 2),
(3, 90, 2, 1),
(4, 29, 2, 2),
(5, 90, 3, 1),
(6, 29, 3, 2);
/*!40000 ALTER TABLE `tbl_point` ENABLE KEYS */;
-- Dumping structure for table student_db.tbl_student
CREATE TABLE IF NOT EXISTS `tbl_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`student_id` varchar(50) DEFAULT NULL,
`sext` varchar(45) DEFAULT NULL,
`dob` date DEFAULT NULL,
`using_class_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_student_tbl_class1_idx` (`using_class_id`),
CONSTRAINT `fk_tbl_student_tbl_class1` FOREIGN KEY (`using_class_id`) REFERENCES `tbl_class` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- Dumping data for table student_db.tbl_student: ~0 rows (approximately)
/*!40000 ALTER TABLE `tbl_student` DISABLE KEYS */;
REPLACE INTO `tbl_student` (`id`, `name`, `student_id`, `sext`, `dob`, `using_class_id`) VALUES
(1, 'sovann', 'G-000 001', 'M', '1993-03-18', 1),
(2, 'rady ny', 'G-000 002', 'M', '1993-04-14', 1),
(3, 'dy sove', 'G-000 003', 'M', '1002-05-28', 1);
/*!40000 ALTER TABLE `tbl_student` ENABLE KEYS */;
-- Dumping structure for table student_db.tbl_subject
CREATE TABLE IF NOT EXISTS `tbl_subject` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`using_class_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_subject_tbl_class1_idx` (`using_class_id`),
CONSTRAINT `fk_tbl_subject_tbl_class1` FOREIGN KEY (`using_class_id`) REFERENCES `tbl_class` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- Dumping data for table student_db.tbl_subject: ~2 rows (approximately)
/*!40000 ALTER TABLE `tbl_subject` DISABLE KEYS */;
REPLACE INTO `tbl_subject` (`id`, `name`, `using_class_id`) VALUES
(1, 'Math', 1),
(2, 'Egnlish', 1);