写一个查询,它将获取每个主题中前3名学生的名字以及他们得分的标记

时间:2017-10-07 11:30:20

标签: oracle plsql

我是PL / SQL新手需要查询以下场景:

2个表:第一个有卷号,名称 第2卷没有,标记为n 需要编写一个查询,该查询将获取每个主题中的前3名学生姓名以及他们得分的标记

我已经在上面写了上面的查询,但它没有得到正确的结果:

SELECT TABLE1.NAME, MAX(TABLE2.MARKS) 
FROM 
TABLE1 
INNER JOIN 
TABLE2
ON TABLE1.ROLL_NO = TABLE2.ROLL_NO
GROUP BY SUB
ORDER BY TABLE2.MARKS
HAVING ROWNUM <3

以下是表格数据:

CREATE TABLE STUDENT ( ROLL_NO INT PRIMARY KEY, NAME VARCHAR(20));
INSERT INTO STUDENT VALUES (1,'NIKHIL');
INSERT INTO STUDENT VALUES (2,'VARUN');
INSERT INTO STUDENT VALUES (3,'NISHANT');
INSERT INTO STUDENT VALUES (4,'VISHAL');
INSERT INTO STUDENT VALUES (5,'GOURAV');
INSERT INTO STUDENT VALUES (6,'HEMANT');
INSERT INTO STUDENT VALUES (7,'SHUBHAM');
INSERT INTO STUDENT VALUES (8,'DHAWAN');
INSERT INTO STUDENT VALUES (9,'GAUTAM');


CREATE TABLE MARKS ( ROLL_NO INT, SUBJECT VARCHAR(20), MARKS INT);
INSERT INTO MARKS VALUES('1','MATHS','92');
INSERT INTO MARKS VALUES('1','SCIENCE','80');
INSERT INTO MARKS VALUES('1','ENGLISH','98');
INSERT INTO MARKS VALUES('1','HINDI','81');
INSERT INTO MARKS VALUES('2','MATHS','89');
INSERT INTO MARKS VALUES('2','SCIENCE','100');
INSERT INTO MARKS VALUES('2','ENGLISH','81');
INSERT INTO MARKS VALUES('2','HINDI','82');
INSERT INTO MARKS VALUES('3','MATHS','98');
INSERT INTO MARKS VALUES('3','SCIENCE','92');
INSERT INTO MARKS VALUES('3','ENGLISH','88');
INSERT INTO MARKS VALUES('3','HINDI','83');
INSERT INTO MARKS VALUES('4','MATHS','88');
INSERT INTO MARKS VALUES('4','SCIENCE','82');
INSERT INTO MARKS VALUES('4','ENGLISH','85');
INSERT INTO MARKS VALUES('4','HINDI','97');
INSERT INTO MARKS VALUES('5','MATHS','94');
INSERT INTO MARKS VALUES('5','SCIENCE','90');
INSERT INTO MARKS VALUES('5','ENGLISH','97');
INSERT INTO MARKS VALUES('5','HINDI','89');
INSERT INTO MARKS VALUES('6','MATHS','89');
INSERT INTO MARKS VALUES('6','SCIENCE','82');
INSERT INTO MARKS VALUES('6','ENGLISH','84');
INSERT INTO MARKS VALUES('6','HINDI','85');
INSERT INTO MARKS VALUES('7','MATHS','82');
INSERT INTO MARKS VALUES('7','SCIENCE','100');
INSERT INTO MARKS VALUES('7','ENGLISH','92');
INSERT INTO MARKS VALUES('7','HINDI','98');
INSERT INTO MARKS VALUES('8','MATHS','99');
INSERT INTO MARKS VALUES('8','SCIENCE','91');
INSERT INTO MARKS VALUES('8','ENGLISH','86');
INSERT INTO MARKS VALUES('8','HINDI','82');
INSERT INTO MARKS VALUES('9','MATHS','100');
INSERT INTO MARKS VALUES('9','SCIENCE','95');
INSERT INTO MARKS VALUES('9','ENGLISH','81');
INSERT INTO MARKS VALUES('9','HINDI','81');

1 个答案:

答案 0 :(得分:1)

您可以像这样使用ROW_NUMBER窗口功能。

 memcpy(&my_number, my_buffer, sizeof(my_number));

demo http://www.sqlfiddle.com/#!4/05776/41