牛客求中位数

求中位数档次

image

drop table if exists class_grade;
CREATE TABLE class_grade (
grade varchar(32) NOT NULL,
number int(4) NOT NULL
);

INSERT INTO class_grade VALUES
('A',2),
('C',4),
('B',4),
('D',2);

这道题的解法没看懂,以后继续看

解法

select grade
from (select grade,
             (select sum(number) from class_grade) as total,
             sum(number) over (order by grade)        a, -- 求正序
             sum(number) over (order by grade desc)   b  -- 求逆序
      from class_grade
      order by grade) t
where a >= total / 2
  and b >= total / 2 -- 正序逆序均大于整个数列数字个数的一半
order by grade;
posted @ 2021-10-12 17:26  临渊不羡渔  阅读(51)  评论(0编辑  收藏  举报