oracle - SQL: Concatenate sequential integer values -


i have column this:

id -------- 1 2 3 4 5 7 10 

and want following resultset:

id -------- 1-5 7 10 

is there way achieve (oracle) sql only?

yes:

select (case when min(id) < max(id)              cast(min(id) varchar2(255)) || '-' || cast(max(id) varchar2(255))              else cast(min(id) varchar2(255))         end) (select id, id - rownum grp       t       order id      ) t group grp order min(id); 

here sql fiddle demonstrating it.

the idea behind query subtracting rownum sequence of numbers results in constant. can use constant grouping.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -