I'm having table records
like this
---- --------- ------
id name points
---- --------- ------
1 aaaa 88
2 bbbb 87
3 cccc 88
4 dddd 87
5 eeee 86
6 ffff 87
7 gggg 87
8 hhhh 85
10 iiii 86
11 iiii 86
12 iiii 86
13 iiii 86
14 iiii 86
15 iiii 86
16 iiii 87
17 iiii 82
18 hhhh 85
form the table, I want to select the records with the following condition
For the first maximum record, Highest Points will be on top. (But only one record). I have two record with the highest point 88, Here the recent one should be displayed (maximum id)
From second record onwards, the fields should be retrieved in the format of (n*2). I mean, for the 2nd maximum records I can allow 4 values to be equal (2*2=4). ie in the table there are several 87s, but I select only recent 4 records (Maximum id). similarly for 3rd maximum, I can allow 3*2=6 records to be same.. and so on..
Currently, I've tried with
select * from records group by points order by points desc
But it limits to only one same record. Is there any possibilities to use count
or any other relevant functions in this query based on the need. Hope you help me.
Edit:
The output should be look like,
---- --------- ------
id name points
---- --------- ------
3 cccc 88 -- maximum (only one)
16 iiii 87 |
6 ffff 87 | -- 2nd maximum (allow 2*2 =4 only)
7 gggg 87 |
4 dddd 87 |
15 iiii 86 |
14 iiii 86 |
13 iiii 86 | -- 3rd maximum (allow 3*2 =6 only)
12 iiii 86 |
11 iiii 86 |
10 iiii 86 | and so on for 4th and 5th
18 hhhh 85
8 hhhh 85
17 iiii 82
I'll leave you (others) to figure out the final part of the problem...
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(12) NOT NULL
,points INT NOT NULL
);
INSERT INTO my_table VALUES
(1 ,'aaaa',88),
(2 ,'bbbb',87),
(3 ,'cccc',88),
(4 ,'dddd',87),
(5 ,'eeee',86),
(6 ,'ffff',87),
(7 ,'gggg',87),
(8 ,'hhhh',85),
(10 ,'iiii',86),
(11 ,'iiii',86),
(12 ,'iiii',86),
(13 ,'iiii',86),
(14 ,'iiii',86),
(15 ,'iiii',86),
(16 ,'iiii',87),
(17 ,'iiii',82);
SELECT x.id
, x.name
, x.points
FROM
( SELECT *
, IF(@prev=points,@i:=@i+1,@i:=1)i
, IF(@prev<>points,@j:=@j+1,@j:=@j)j
, @prev:=points
FROM my_table
, (SELECT @i:=0,@j:=0,@prev:='')vars
ORDER
BY points DESC
, id DESC
) x
WHERE i <= j*2;
+----+------+--------+
| id | name | points |
+----+------+--------+
| 3 | cccc | 88 |
| 1 | aaaa | 88 |
| 16 | iiii | 87 |
| 7 | gggg | 87 |
| 6 | ffff | 87 |
| 4 | dddd | 87 |
| 15 | iiii | 86 |
| 14 | iiii | 86 |
| 13 | iiii | 86 |
| 12 | iiii | 86 |
| 11 | iiii | 86 |
| 10 | iiii | 86 |
| 8 | hhhh | 85 |
| 17 | iiii | 82 |
+----+------+--------+
/*Sample data*/
CREATE TABLE t
(`id` int, `name` varchar(9), `points` int)
;
INSERT INTO t
(`id`, `name`, `points`)
VALUES
('1', 'aaaa', '88'),
('2', 'bbbb', '87'),
('3', 'cccc', '88'),
('4', 'dddd', '87'),
('5', 'eeee', '86'),
('6', 'ffff', '87'),
('7', 'gggg', '87'),
('8', 'hhhh', '85'),
('10', 'iiii', '86'),
('11', 'iiii', '86'),
('12', 'iiii', '86'),
('13', 'iiii', '86'),
('14', 'iiii', '86'),
('15', 'iiii', '86'),
('16', 'iiii', '87'),
('17', 'iiii', '82'),
('18', 'hhhh', '85')
;
/*Query*/
SELECT id, name, points FROM (
SELECT
t.*
, @n := IF(@prev_points != points, @n + 1, @n) AS n
, @row := IF(@prev_points != points, 1, @row + 1) AS row
, @prev_points := points
FROM
t
, (SELECT @prev_points := null, @n := 1, @row := 0) var_init_subquery
ORDER BY points DESC, id DESC
) sq
WHERE row <= CASE WHEN n = 1 THEN 1 ELSE n * 2 END
;
/*Result*/
| ID | NAME | POINTS |
|----|------|--------|
| 3 | cccc | 88 |
| 16 | iiii | 87 |
| 7 | gggg | 87 |
| 6 | ffff | 87 |
| 4 | dddd | 87 |
| 15 | iiii | 86 |
| 14 | iiii | 86 |
| 13 | iiii | 86 |
| 12 | iiii | 86 |
| 11 | iiii | 86 |
| 10 | iiii | 86 |
| 18 | hhhh | 85 |
| 8 | hhhh | 85 |
| 17 | iiii | 82 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.