将连续数字计算到一行

时间:2013-05-16 01:29:42

标签: oracle

我最近发现我们可以使用Oracle的Max(Decode())函数来旋转表的结果。我已经执行了这个很好。但是,转动表只是我需要的解决方案的一部分。旋转功能会产生如下结果:

01,02,03,05,06,07,08,09,10,11,12,13,14,16,17,20,21,23,25

我真正需要的是这样的事情:

1-17, 20-21,23,25

我真的不知道如何开始解决这个问题,但到目前为止,我有以下问题:

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

感谢帮助人们。

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT
 MAX(CASE WHEN wldw.wafernumber BETWEEN '01' AND '17' THEN '01-17' END)
  || MAX(DECODE(wldw.wafernumber,'18', ',18')) 
  || MAX(DECODE(wldw.wafernumber,'19', ',19')) 
  || MAX(CASE WHEN wldw.wafernumber IN ('20','21') THEN '20-21' END) 
  || 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