Oracle:为范围(sysyear + 9)内缺失的年份生成行

8tntrjer  于 2023-01-30  发布在  Oracle
关注(0)|答案(2)|浏览(132)

我有一个Oracle 18c表,其中包含特定年份的行:

with data (year_, amount) as (
select 2024, 100 from dual union all
select 2025, 200 from dual union all
select 2025, 300 from dual union all
select 2026, 400 from dual union all
select 2027, 500 from dual union all
select 2028, 600 from dual union all
select 2028, 700 from dual union all
select 2028, 800 from dual union all
select 2029, 900 from dual union all
select 2031, 100 from dual
)
select
    *
from
    data

     YEAR_     AMOUNT
---------- ----------
      2024        100
      2025        200
      2025        300
      2026        400
      2027        500
      2028        600
      2028        700
      2028        800
      2029        900
      2031        100

我希望在此范围内每年至少有一行:sysyear + 9。换句话说,我希望从当前年份(当前为2023年)开始,有10年的行。
我有几年没看电影了:2023年、2030年和2032年。因此,我希望为这些缺失的年份生成填充行。填充行的amount应为null
它看起来像这样:

YEAR_     AMOUNT
---------- ----------
      2023             --filler
      2024        100
      2025        200
      2025        300
      2026        400
      2027        500
      2028        600
      2028        700
      2028        800
      2029        900
      2030             --filler
      2031        100
      2032             --filler

在Oracle SQL查询中,如何选择行并生成10年范围内的填充行?

    • 编辑:**我不希望在查询或表中手动创建年份列表。我希望在查询中创建动态范围。
wz1wpwve

wz1wpwve1#

试试看:

Select    y.YEAR_, t.AMOUNT
From      (Select EXTRACT(YEAR From SYSDATE) + LEVEL - 1 "YEAR_" From Dual Connect By LEVEL <= 10) y
Left Join tbl t ON(t.YEAR_ = y.YEAR_)
Order By  y.YEAR_, t.AMOUNT

使用示例数据:

WITH 
    tbl (YEAR_, AMOUNT) AS 
        (
            Select 2024, 100 From Dual Union All
            Select 2025, 200 From Dual Union All
            Select 2025, 300 From Dual Union All
            Select 2026, 400 From Dual Union All
            Select 2027, 500 From Dual Union All
            Select 2028, 600 From Dual Union All
            Select 2028, 700 From Dual Union All
            Select 2028, 800 From Dual Union All
            Select 2029, 900 From Dual Union All
            Select 2031, 100 From Dual
        )

......结果是:
| 年份|金额|
| - ------|- ------|
| 二○二三||
| 小行星2024|一百|
| 小行星2025|二百|
| 小行星2025|三百|
| 二○二六|四百|
| 小行星2027|五百|
| 二○二八|六百|
| 二○二八|七百|
| 二○二八|八百|
| 二○二九|九百|
| 小行星2030||
| 小行星2031|一百|
| 二○三二||

dgtucam1

dgtucam12#

我将CTE分解为多个位来帮助解释,但这应该可以解决问题

SQL> with data (year_, amount) as (
  2  select 2024, 100 from dual union all
  3  select 2025, 200 from dual union all
  4  select 2025, 300 from dual union all
  5  select 2026, 400 from dual union all
  6  select 2027, 500 from dual union all
  7  select 2028, 600 from dual union all
  8  select 2028, 700 from dual union all
  9  select 2028, 800 from dual union all
 10  select 2029, 900 from dual union all
 11  select 2031, 100 from dual
 12  ),
 13  boundaries as (
 14  select max(year_) maxy, min(year_) miny
 15  from data
 16  ),
 17  all_the_years as
 18  ( select miny+rownum-1 yr
 19    from boundaries
 20    connect by level <= maxy-miny+1
 21  )
 22  select *
 23  from  all_the_years a
 24   left outer join data d
 25  on ( a.yr = d.year_ )
 26  order by 1;

        YR      YEAR_     AMOUNT
---------- ---------- ----------
      2024       2024        100
      2025       2025        200
      2025       2025        300
      2026       2026        400
      2027       2027        500
      2028       2028        700
      2028       2028        800
      2028       2028        600
      2029       2029        900
      2030
      2031       2031        100

11 rows selected.

如果固定为10年,则不需要MAX -只需连接〈= 10的级别

相关问题