TestResultテーブルのPointの順位(DENSE)が、 16位から20位のデータを出力する 出力結果 Point Rank ----- ---- 53 16 52 17 51 18 43 19 42 20
create table TestResult(Point int); insert into TestResult(Point) values (99),(98),(97),(89),(88), (87),(79),(78),(77),(69), (68),(67),(59),(58),(57), (53),(52),(51),(43),(42), (41),(33),(32),(31),(23), (22),(21),(13),(12),(11);
#■■■インラインビューを使わない方法■■■ select Point, (select count(distinct b.Point) from TestResult b where b.Point > a.Point)+1 as Rank from TestResult a where (select count(distinct b.Point) from TestResult b where b.Point > a.Point)+1 between 16 and 20 order by Rank; #■■■インラインビューを使う方法■■■ select Point,Rank from (select Point, (select count(distinct b.Point) from TestResult b where b.Point > a.Point)+1 as Rank from TestResult a) dummy where Rank between 16 and 20 order by Rank;
Count関数にdistinctを指定して、 DENSEな順位を取得してます