Tuesday, 15 July 2014

sql - Fetch set or rows from table -



sql - Fetch set or rows from table -

i have table exam_record data. need know how pullout latest 2 record each eid based on latest exam date.

eid | exam_name | score | date_of_completion | ----------------------------------------------- 1 | exam_1 | 60 | 23-jun-2014 | 1 | exam_1 | 70 | 10-jan-2014 | 1 | exam_1 | 71 | 15-aug-2014 | 1 | exam_1 | 65 | 1-sep-2014 | 2 | exam_2 | 50 | 2-jul-2014 | 2 | exam_2 | 55 | 12-may-2014 | 2 | exam_2 | 65 | 15-apr-2014 |

desired output is

eid | exam_name | score | date_of_completion | ----------------------------------------------- 1 | exam_1 | 71 | 15-aug-2014 | 1 | exam_1 | 65 | 1-sep-2014 | 2 | exam_2 | 55 | 12-may-2014 | 2 | exam_2 | 50 | 2-jul-2014 |

try this:

select * ( select *,row_number()over(partition eid order date_of_completion desc) rn table )x x<=2

more info

sql oracle plsql

No comments:

Post a Comment