【问题标题】:Unable to execute Oracle Select Stmt无法执行 Oracle Select Stmt
【发布时间】:2021-12-02 15:51:08
【问题描述】:

已通过建议的更改和遇到的新错误更新了问题, 我正在执行这段代码

SELECT
    b.*, 
    COUNT(CASE WHEN DOJ <= current_date 
                    AND (DOL IS NULL OR DOL >= current_date) 
                  THEN ID END) FIRST 
FROM
    (
     select ID,max(case when a.massn in('00','A1') then a.begda end) as DOJ,
     max(case when a.massn in ('A4') then a.begda end) as DOP,      
     max(case when a.massn in ('A3','A5') then a.begda end) as DOT,       
     max(case when a.massn='A6' then a.begda end) dol,
     max(case when a.massn in ('A6') then a.aedtm end) as changed_on_date,     
     max(case when a.massn in ('A6') then massn end) as massn,
     max(case when a.massn in ('A6') then massg end) as massg 
    from "Table"."T1" a 
     group by ID
  )b
  WHERE FIRST = 1

我收到一个错误:

ORA-01861: 文字与格式字符串不匹配 01861. 00000 - “文字与格式字符串不匹配” *原因:输入中的文字必须与输入中的文字长度相同 格式字符串(前导空格除外)。如果 “FX”修饰符已打开,文字必须完全匹配, 没有额外的空格。 *操作:更正格式字符串以匹配文字。

我们将不胜感激任何帮助

【问题讨论】:

  • 您要查询哪些表?
  • FIRST 是 Oracle 中的一个函数(保留关键字),但在这里您尝试将其用作别名。如果您想将其用作列名/别名,请在 "FIRST" 周围添加一些双引号。您还需要在 WHERE 子句中将双引号添加到 FIRST
  • 当前抛出的错误(可能还有其他错误)是由于选择了*以及其他原因造成的。选择“所有列”其他内容的唯一方法是在from 子句中给表一个别名,例如:from my_table t,然后在select 子句中使用它像这样:select t.*, ... 除此之外,from () 是什么意思?您肯定没有名为() 的表格,那您为什么要展示它?你需要帮助吗?
  • @JNevill,不是真的。您可以这样使用first。它是一个关键字,而不是一个保留字
  • 我的错误。那么一定是@Mathguy 的出色表现:)

标签: sql oracle


【解决方案1】:

认为这就是你所追求的。

SQL> with
  2  test (id, doj, dol) as
  3  -- sample data
  4    (select 100, sysdate - 10, sysdate + 5 from dual union all
  5     select 200, sysdate     , sysdate - 8 from dual
  6    ),
  7  -- new CTE that counts rows
  8  tcount as
  9    (select t.id, t.doj, t.dol,
 10       count(case when doj <= current_date and (dol is null or dol >= current_date) then id end) first
 11     from test t
 12     group by t.id, t.doj, t.dol
 13    )
 14  -- finally
 15  select *
 16  from tcount
 17  where first = 1;

        ID DOJ                 DOL                      FIRST
---------- ------------------- ------------------- ----------
       100 22.11.2021 19:45:43 07.12.2021 19:45:43          1

SQL>

它有什么作用?

  • 第 1 - 5 行 - 样本数据;您已经拥有该表并被 () 错误识别
  • 第 6 - 10 行 - 从表中选择行,以及 count 函数
  • 最后,选择count函数返回1的行

【讨论】:

    【解决方案2】:

    我发现,根据 Oracle 语法,我的日期格式是错误的,

    以下代码对我有用,

    TO_CHAR(TO_DATE(a.begda,'YYYYMMDD'), 'DD-MM-YYYY')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多