I have to get the row with longest string value for same id fields.
create table test(
id number,
test_data varchar2(20)
);
insert all
into test values (1,'aaa')
into test values (1,'a')
into test values (1,'abxw')
into test values (2,'aaa')
into test values (2,'tris')
select * from dual;
my desired output is
1 abxw –longest string
2 tris
how can I get the required output?? I am not getting any idea.
guys what about using cursor. can we use cursor for this purpose?? Does anyone have any idea? Is it possible??
Thank You.
I like using partition for these kind of queries:
http://www.sqlfiddle.com/#!4/66d4c/20
Of course, the nice thing about this is that if you decide that you want another tiebreak (e.g., alphabetically), you just need to add that to your order by clause. Which, by the way is not a bad idea, that way your result set will not be non-deterministic.