Consider this table with this data:
create table responses_v5 ( user_id varchar2(20), hw_id number(38), pb_id number(38), tries number(38), grade number(3,2), response varchar2(20), primary key(user_id, hw_id, pb_id, tries) );
insert into responses_v5 values ( 'bob', 4540, 1, 1, 0.0, 'response1' );
insert into responses_v5 values ( 'bob', 4540, 1, 2, 0.0, 'response2' );
insert into responses_v5 values ( 'bob', 4540, 1, 3, 1.0, 'response3' );
insert into responses_v5 values ( 'refoyl', 4540, 1, 1, 0.0, 'response4' );
insert into responses_v5 values ( 'refoyl', 4540, 1, 2, 0.0, 'response5' );
insert into responses_v5 values ( 'refoyl', 4540, 2, 1, 0.0, 'response6' );
insert into responses_v5 values ( 'refoyl', 4540, 2, 2, 0.0, 'response7' );
insert into responses_v5 values ( 'refoyl', 4540, 2, 3, 1.0, 'response8' );
insert into responses_v5 values ( 'refoyl', 4541, 1, 1, 0.0, 'response9' );
insert into responses_v5 values ( 'refoyl', 4541, 1, 2, 0.0, 'response0' );
For each unique combination of user_id, hw_id, and pb_id, I want only the record that has the maximum number of tries. Such a query would return only these records from the table above:
'bob', 4540, 1, 3, 1.0, 'response3'
'refoyl', 4540, 1, 2, 0.0, 'response5'
'refoyl', 4540, 2, 3, 1.0, 'response8'
'refoyl', 4541, 1, 2, 0.0, 'response0'
Currently I am acquiring all the records, ordering them by user_id, hw_id, pb_id, and -tries, and then using Java to save the first record for each unique combination of user_id, hw_id, and pb_id and discarding the remaining ones. But clearly it would be better if I did not acquire those extra records in the first place.
Any ideas?