繁体   English   中英

获取 Ora-00903:在 Oracle 中创建 CTE 日历时表名无效

[英]Getting Ora-00903: Invalid table name when creating CTE calendar in Oracle

这是我的代码:

with calendar as
(
  select 
    to_date('1-jan-2019') + daynum as day_of_year, 
    Day_of_Week,
    rownum as Date_ID
  from 
    (
      select 
        rownum - 1 as daynum,
        to_char(to_date('1-jan-2019') + rownum - 1,'D') as Day_of_Week
      from 
        dual connect by rownum < sysdate - to_date('1-jan-2019') + 1000)
      where 
        day_of_week not in (1,7)
    ),

我需要知道为什么我会收到这个错误以及如何解决它。 当我在没有“使用日历为”的情况下运行它时

select to_date('1-jan-2019') + daynum as day_of_year,
  Day_of_Week,rownum as Date_ID
  from 
  (
    select 
    rownum - 1 as daynum,
    to_char(to_date('1-jan-2019') + rownum - 1,'D') as Day_of_Week
    from dual connect by rownum < sysdate - to_date('1-jan-2019') + 1000)
    where day_of_week not in (1,7)

它运行,结果是

结果

后端故事:所以,我正在为我们的 SLA(服务级别协议)创建规则。 在我们的仓库中,我们必须在一定天数内将订单送到码头。 因此,通过生成日历并添加下面的“SLA”规则,我们可以获得一个“by_date”,显示我们需要在哪一天将它放在码头上。 当订单下降时。

select distinct ord_num, max(sla) as sla from (
          select 
          h.ord_num,d10a.EDI_DATA_ID_VALUE as Order_TP, d10b.EDI_DATA_ID_VALUE as Augment,
            case when
            LOWER(d10b.EDI_DATA_ID_VALUE) LIKE '%shroud%' then 10
              when ord_lev1 in ('207347') then 4
              --Charles Cabinet
              when ord_lev1 in ('204611','204816','204819','205333','205818','205988') then 3
              --All Other cabinet IMNs or spare orders to cabinet
              when d10a.EDI_DATA_ID_VALUE = 'spare' then 0
              --SLA on spares, have to account for start date in another CTE
              else
              2
              end as sla --all other orders that arent charles, cabinet or a spare
      from e_ord_h h
      left join e_ord_d5 d5 on h.ord_num = d5.ord_num and d5.comp_code = 'S1'
      left join e_ord_D10 d10a on h.ord_num = d10a.ord_num and d10a.EDI_DATA_ID_DES = 'Capstan Order Type'and d10a.comp_code = 'S1'
      left join e_ord_D10 d10b on h.ord_num = d10b.ord_num and d10b.EDI_DATA_ID_DES = 'Capstan Augment'and d10b.comp_code = 'S1'
      where h.comp_code = 'S1' and flow_pros_code <> 'COOR' and ord_Stat = 'A'
      ) group by ord_num

它对我有用。 不过,您实际上并没有分享您的整个查询,所以我只是假设您分享的内容是最重要的。

数据库 12.2

with calendar as
(
  select 
    to_date('1-jan-2019') + daynum as day_of_year, 
    Day_of_Week,
    rownum as Date_ID
  from 
    (
      select 
        rownum - 1 as daynum,
        to_char(to_date('1-jan-2019') + rownum - 1,'D') as Day_of_Week
      from 
        dual connect by rownum < sysdate - to_date('1-jan-2019') + 1000)
      where 
        day_of_week not in (1,7)
    )
select * from calendar;

在此处输入图片说明

这是您目前拥有的:

SQL> with calendar as
  2  (
  3    select
  4      to_date('1-jan-2019') + daynum as day_of_year,
  5      Day_of_Week,
  6      rownum as Date_ID
  7    from
  8      (
  9        select
 10          rownum - 1 as daynum,
 11          to_char(to_date('1-jan-2019') + rownum - 1,'D') as Day_of_Week
 12        from
 13          dual connect by rownum < sysdate - to_date('1-jan-2019') + 1000)
 14        where
 15          day_of_week not in (1,7)
 16      ),
 17  /
    ),
     *
ERROR at line 16:
ORA-00903: invalid table name


SQL>

16号线? 那是

 16      ),

看到任何可疑的东西? 我愿意。 逗号在这里无效,但是 - 这是有效的:

SQL> with calendar as
  2  (
  3    select
  4      to_date('1-jan-2019') + daynum as day_of_year,
  5      Day_of_Week,
  6      rownum as Date_ID
  7    from
  8      (
  9        select
 10          rownum - 1 as daynum,
 11          to_char(to_date('1-jan-2019') + rownum - 1,'D') as Day_of_Week
 12        from
 13          dual connect by rownum < sysdate - to_date('1-jan-2019') + 1000)
 14        where
 15          day_of_week not in (1,7)
 16      )
 17  select * From calendar
 18  where rownum < 5;

DAY_OF_YEAR D    DATE_ID
----------- - ----------
01-jan-2019 2          1
02-jan-2019 3          2
03-jan-2019 4          3
04-jan-2019 5          4

SQL>

where是这里只是缩短输出)。

使用没有格式说明符的TO_DATE()总是有风险的。 正确使用将是

to_date('1-jan-2019', 'dd-mon-yyyy', 'NLS_DATE_LANGUAGE = american')

或更简单的 DATE 文字: DATE '2019-01-01'

TO_CHAR(..., 'D')取决于当前用户会话NLS_TERRITORY设置,它可能随时更改。 如果你想排除周六和周日,最好使用这个:

TO_CHAR(DATE '2019-01-01' + rownum, 'Dy', 'NLS_DATE_LANGUAGE = american') NOT IN ('Sat','Sun')

这将在任何条件下工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM