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

时间:2017-12-27 04:12:22

标签: db2

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
  13. 请让我知道你的想法。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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)

答案 3 :(得分: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