題目:查詢每班前兩名學生的資訊

來源:互聯網
上載者:User

今天偶然想起一個群曾經出的一道sql題
然後一搜群聊都是2月8號的事了


題目 資料表結構為 


create table Ques1 ( id int identity(1,1) primary key,--主鍵 [Name] nvarchar(50) not null,--學生姓名 Score int not null,--學產生績 Class int not null--學生班級 )


要查詢每班前兩名學生的資訊

用一條sql搞定


答案是:



SELECT *   FROM (SELECT ST.ID, ST.GRADE, ST.NAME, ST.SCORE           FROM STUDENTS ST           JOIN (SELECT MAX(SCORE) AV, GRADE FROM STUDENTS GROUP BY GRADE) T ON ST.GRADE =                                                                                T.GRADE          WHERE ST.SCORE = T.AV                 UNION ALL                 SELECT ST.ID, ST.GRADE, ST.NAME, ST.SCORE           FROM STUDENTS ST           JOIN (SELECT MAX(SCORE) AV, GRADE                   FROM (SELECT ST.ID, ST.GRADE, ST.NAME, ST.SCORE, T.AV                           FROM STUDENTS ST                           JOIN (SELECT MAX(SCORE) AV, GRADE                                  FROM STUDENTS                                 GROUP BY GRADE) T ON ST.GRADE = T.GRADE                          WHERE ST.SCORE != T.AV)                  GROUP BY GRADE) T ON ST.GRADE = T.GRADE          WHERE ST.SCORE = T.AV) ORDER BY GRADE 

上面的sql比較複雜,今天想起用視窗函數輕鬆實現
資料庫postgreSQL(不知道是什麼資料庫的可以去搜下)


drop table if exists students;-- 建表create table students ( id serial primary key, --主鍵 names varchar(50) not null, --學生姓名score int not null, --學產生績class int not null --學生班級);-- 插入測試資料insert into students (names, score, class) values('龍神', 61, 1),('書包', 59, 1),('命運', 72, 1),('roy', 80, 2),('相思', 77, 2),('c', 70, 2),('小手', 70, 3),('桶桶', 80, 3),('一姐', 90, 3),('夏至', 60, 4),('tony', 70, 4),('阿龍', 80, 4);select * from students; -- 查看錶資料-- 得到每班前兩名學生的資訊select * from (select row_number() over (partition by class order by score desc ) as num,* from students) t where t.num <= 2;

這樣就輕鬆搞定了,結果




再看MSSQL


 IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='students') drop table Ques1;  -- 建表 create table students (  id int identity(1,1) primary key,--主鍵  names nvarchar(50) not null,--學生姓名  score int not null,--學產生績  class int not null--學生班級  ); -- 插入測試資料 insert into students (names, score, class) values('龍神', 61, 1),('書包', 59, 1),('命運', 72, 1),('roy', 80, 2),('相思', 77, 2),('c', 70, 2),('小手', 70, 3),('桶桶', 80, 3),('一姐', 90, 3),('夏至', 60, 4),('tony', 70, 4),('阿龍', 80, 4);-- 查看錶資料select * from students;-- 得到每班前兩名學生的資訊select * from (select row_number() over (partition by class order by score asc ) as num,* from students) t where t.num <= 2;

結果

row_number() 是一視窗函數 各大資料庫應該都是有的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.