title: Counting consecutive numbers into one row
body: <p>I have recently discovered that we can use Max ( Decode ()) function of Oracle to pivot the results of a table. I have executed this just fine. However, pivoting a table is just one part of the solution that I need. The pivoting function results to something like this:</p>
  
  <pre><code>01,02,03,05,06,07,08,09,10,11,12,13,14,16,17,20,21,23,25
  </code></pre>
  
  <p>What I actually need is something like this:</p>
  
  <pre><code>1-17, 20-21,23,25
  </code></pre>
  
  <p>I really don't know how to start solving this but so far I have the below query:</p>
  
  <pre><code>SELECT DISTINCT
       MAX(DECODE(wldw.wafernumber,'01', '01')) 
        || MAX(DECODE(wldw.wafernumber,'02', ',02')) 
        || MAX(DECODE(wldw.wafernumber,'03', ',03')) 
        || MAX(DECODE(wldw.wafernumber,'04', ',04')) 
        || MAX(DECODE(wldw.wafernumber,'05', ',05')) 
        || MAX(DECODE(wldw.wafernumber,'06', ',06')) 
        || MAX(DECODE(wldw.wafernumber,'07', ',07')) 
        || MAX(DECODE(wldw.wafernumber,'08', ',08')) 
        || MAX(DECODE(wldw.wafernumber,'09', ',09')) 
        || MAX(DECODE(wldw.wafernumber,'10', ',10')) 
        || MAX(DECODE(wldw.wafernumber,'11', ',11')) 
        || MAX(DECODE(wldw.wafernumber,'12', ',12')) 
        || MAX(DECODE(wldw.wafernumber,'13', ',13')) 
        || MAX(DECODE(wldw.wafernumber,'14', ',14')) 
        || MAX(DECODE(wldw.wafernumber,'15', ',15')) 
        || MAX(DECODE(wldw.wafernumber,'16', ',16')) 
        || MAX(DECODE(wldw.wafernumber,'17', ',17')) 
        || MAX(DECODE(wldw.wafernumber,'18', ',18')) 
        || MAX(DECODE(wldw.wafernumber,'19', ',19')) 
        || MAX(DECODE(wldw.wafernumber,'20', ',20')) 
        || MAX(DECODE(wldw.wafernumber,'21', ',21')) 
        || MAX(DECODE(wldw.wafernumber,'22', ',22')) 
        || MAX(DECODE(wldw.wafernumber,'23', ',23')) 
        || MAX(DECODE(wldw.wafernumber,'24', ',24')) 
        || MAX(DECODE(wldw.wafernumber,'25', ',25'))  AS WAFERS      
        , 'history' as well
  FROM a_wiplothistory wl
  JOIN Container C ON (wl.containerid = c.containerid OR wl.containerid= c.splitfromid )
  JOIN a_wiplotdetailshistory wld ON wl.wiplothistoryid = wld.wiplothistoryid
  JOIN a_wiplotdetailswafershistory wldw ON wld.wiplotdetailshistoryid = wldw.wiplotdetailshistoryid
  WHERE c.containername = :lot
  </code></pre>
  
  <p>Thanks for helping guys.</p>