仅选择序列

时间:2017-08-04 14:34:11

标签: db2-400

我想在我的表格中选择一个部分,在我阅读00006之后,当我读取下一个模式00XXX时停止选择。(这里是00040,但它可能是另一个数字)

00006 
123456
456789
123789
00040
125478
547896
454623
519846
00001
....

这里,例如我想只用select:

获取这些值

123456 456789 123789

如何获得?我找不到任何线索。

感谢您的帮助。

ps:我没有权利转换我选择的表格

使用Charles的解决方案:

with marker as (
   select rownumber() over() as rowno
          , rrn(t) as recno, cast(substr(YYYYYY, 1, 5)as integer) as markvalue
   from fap1t010.£$ZZZZZZ t
   where substr(YYYYYY, 1, 5) like ('00___')
 ), dataa as (
   select rrn(t) as recno, cast(substr(YYYYYY, 1, 6)as integer) as datavalue
   from fap1t010.£$ZZZZZZ t
   where substr(YYYYYY, 1, 5) NOT like ('00006')
   and substr(YYYYYY, 13, 1) ='C'
), ranges as(
   select a.markvalue
         , a.recno as startrec
         , b.recno as endrec
   from marker A join marker B
     on b.rowno = a.rowno +1
)
 select distinct d.datavalue  
 from ranges R join dataa D
      on d.recno between r.startrec and r.endrec
 where r.markvalue = 00006

1 个答案:

答案 0 :(得分:1)

哇...这是一个丑陋的要求。

但是可以做到:

with marker as (
   select rownumber() over() as rowno
          , rrn(t) as recno, myfld as markvalue
   from dtcwilt.temp t
   where myfld like ('00___ ')
 ), data as (
   select rrn(t) as recno, myfld as datavalue
   from dtcwilt.temp t
   where myfld NOT like ('00___ ')
), ranges as(
   select a.markvalue
         , a.recno as startrec
         , b.recno as endrec
   from marker A join marker B
     on b.rowno = a.rowno +1
)
 select d.datavalue  
 from ranges R join data D
      on d.recno between r.startrec and r.endrec
 where r.markvalue = '00006';

如果你有一个庞大的数据集,性能就不会那么好;可能还有改进的余地。但上述内容至少很容易理解。

如果您碰巧使用的是IBM i的最新版本7.3,则可以通过使用新的LEAD()和/或LAG()函数来简化声明。但我没有7.3测试。