简体   繁体   中英

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

Here is my code:

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)
    ),

I need to know why I am getting this error and how to fix it. When I run it without '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)

It runs, and results are

Results

Backend Story: So, I am creating a rule for our SLA (Service Level Agreement). In our warehouse, we have to a certain amount of days to get orders to the dock. So by generating a calender and adding the "SLA" rule below, we can get a 'by_date' showing what day we need to have this on the dock Vs. when the order dropped.

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

It's working for me. You didn't actually share your entire query though, so I just assumed what you did share was what was most important.

Database 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;

在此处输入图片说明

This is what you currently have:

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>

Line 16? That's

 16      ),

See anything suspicious? I do. Comma is invalid here, but - this is valid:

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 is here just to shorten output).

It is always risky to use TO_DATE() without a format specifier. Proper use would be

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

or simpler with DATE literal: DATE '2019-01-01'

The result of TO_CHAR(..., 'D') depends on the current user session NLS_TERRITORY setting, it may change at any time. If you like to exclude Saturday and Sunday better use this:

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

This will work under any condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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