DB2 Query将1到12的月份数作为12行

问题描述 投票:-3回答:4

DB2 Query将月份编号从1到12作为12行。

我想要的结果如下

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12

请让我知道你的想法。

db2
4个回答
1
投票

common table expression(WITH子句)允许您定义要从中进行选择的表。可以完成以下内容:

with dummy(id) as (
    select 1 from SYSIBM.SYSDUMMY1    
    union all
    select id + 1 from dummy where id < 12
)
select id from dummy

0
投票

我想你的所有模式中的所有表都有+ 12列,你可以这样做:

select rownumber() over() as mymonth from syscolumns
fetch first 12 rows only

0
投票

其他方法:

with AllMonth(mymonth) as (
    values (1), (2), (3), (4),(5), (6),(7), (8), (9), (10),(11), (12)
)
select mymonth from AllMonth

或不与:

select mymonth from (
values (1), (2), (3), (4),(5), (6),(7), (8), (9), (10),(11), (12)
) AllMonth(mymonth)

0
投票

其他方法:

with AllMonth(mymonth) as (
Select 1 from SYSIBM.SYSDUMMY1 
union all
Select 2 from SYSIBM.SYSDUMMY1 
union all
Select 3 from SYSIBM.SYSDUMMY1 
union all
Select 4 from SYSIBM.SYSDUMMY1 
union all
Select 5 from SYSIBM.SYSDUMMY1 
union all
Select 6 from SYSIBM.SYSDUMMY1 
union all
Select 7 from SYSIBM.SYSDUMMY1 
union all
Select 8 from SYSIBM.SYSDUMMY1 
union all
Select 9 from SYSIBM.SYSDUMMY1 
union all
Select 10 from SYSIBM.SYSDUMMY1 
union all
Select 11 from SYSIBM.SYSDUMMY1 
union all
Select 12 from SYSIBM.SYSDUMMY1 
)
select mymonth from AllMonth
© www.soinside.com 2019 - 2024. All rights reserved.