यह आपका वांछित परिणाम देगा:
select
id,
ayear,
byear,
yeardiff
from
(
select
a.id,
a.year ayear,
b.year byear,
(b.year - a.year)+1 yeardiff,
dense_rank() over (partition by a.id order by (b.year - a.year) desc) rank
from
years a
join years b on a.id = b.id
and b.year > a.year
where
b.year - a.year =
(select count(*)-1
from years a1
where a.id = a1.id
and a1.year between a.year and b.year)
)
where
rank = 1
संपादित करें सबसे लंबे खिंचाव के प्रारंभ/समाप्ति वर्ष प्रदर्शित करने के लिए अपडेट किया गया।
SQLFiddle