SQL transformation


SQL transformation

how can we convert single row to multiple row (example)

SAP_SALES_ORD_NUM COVERAGE_TERM
1122 3
 
I need result as
 
SAP_SALES_ORD_NUM COVERAGE_TERM
1122 1
1122 2
1122 3



with temp as(
select 1122 as SAP_SALES_ORD_NUM , 3 COVERAGE_TERM from sysibm.sysdummy1 ),
loops (SAP_SALES_ORD_NUM ,COVERAGE_TERM)  as(
select SAP_SALES_ORD_NUM ,COVERAGE_TERM from temp 
union all
select l.SAP_SALES_ORD_NUM ,case when l.COVERAGE_TERM > 1 then l.COVERAGE_TERM - 1 end as COVERAGE_TERM from loops l  ,temp t where t.SAP_SALES_ORD_NUM = l.SAP_SALES_ORD_NUM and l.COVERAGE_TERM >=1 
)
select * from loops where COVERAGE_TERM is not null 

 SAP_SALES_ORD_NUM     COVERAGE_TERM
 -----------------     -------------
              1122                3
              1122                2
              1122                1

原文地址:https://www.cnblogs.com/TendToBigData/p/10501261.html