【问题标题】:invalid relational operator in SQLSQL 中的无效关系运算符
【发布时间】:2015-08-06 23:39:44
【问题描述】:
select
create_date
,resolved_date
,to_char(create_date, 'YYYY') as year_create
,to_char(create_date, 'MM') as month_create
,to_char(create_date, 'WW') as week_create
,to_char(create_date,'Q') as quarter_create
,to_char(resolved_date, 'YYYY') as year_resolved
,to_char(resolved_date, 'MM') as month_resolved
,to_char(resolved_date, 'WW') as week_resolved
,to_char(resolved_date,'Q') as quarter_resolved
,item
,site
,status
,contact_time
,impact_label

from mytable

where
create_date between to_date('2013/03/01','YYYY/MM/DD') and to_date('2015/08/06','YYYY/MM/DD')
and case item 
when '1' then 'a'
when '2' then 'b'
when '3' then 'c'
else null 
end 

group by
create_date
,resolved_date
,to_char(create_date, 'YYYY') 
,to_char(create_date, 'MM') 
,to_char(create_date, 'WW') 
,to_char(create_date,'Q') 
,to_char(resolved_date, 'YYYY') 
,to_char(resolved_date, 'MM') 
,to_char(resolved_date, 'WW') 
,to_char(resolved_date,'Q') 
,item
,site
,status
,contact_time
,impact_label

order by item, site, create_date;

谁能帮我找出错误

无效的关系运算符

(它在一行一行中表示)。我没有看到我可能遗漏的地方><= ?

【问题讨论】:

  • 首先,你为什么有一个group by 子句?您似乎没有进行任何聚合。其次,在您的where 子句中,您有一个case 语句,但您没有将该语句的结果与任何内容进行比较。我不知道你想用 case 声明来完成什么,所以很难猜出你的意图。
  • 1.我认为 group by 在任何情况下都是必要的,如果没有,我可以尝试将其删除。 2.在我的case语句中,我试图为我的item列生成不同的结果,所以如果它的结果​​为1 - >它将显示a ...这实际上不是我的case语句,我只是编造的一个例子。谢谢!
  • A group by 仅在您希望将行分组并聚合数据时使用。听起来您可以删除group by。如果您希望 case 语句更改显示的内容,您希望它在 select 子句中作为投影中的附加列,而不是在 where 子句中。
  • techonthenet.com/sql/group_by.php 不是聚合函数所必需的分组方式吗?
  • 没有。当您的查询在某些列上包含聚合函数时,您使用 group by。否则,对行进行分组是没有意义的。您可以包含不带聚合函数的group by。它只是无缘无故地减慢查询速度,并使支持变得更加痛苦。

标签: oracle operator-keyword relational


【解决方案1】:

您的案例结构不合理。试试这个:

SELECT create_date,
         resolved_date,
         TO_CHAR (create_date, 'YYYY') AS year_create,
         TO_CHAR (create_date, 'MM') AS month_create,
         TO_CHAR (create_date, 'WW') AS week_create,
         TO_CHAR (create_date, 'Q') AS quarter_create,
         TO_CHAR (resolved_date, 'YYYY') AS year_resolved,
         TO_CHAR (resolved_date, 'MM') AS month_resolved,
         TO_CHAR (resolved_date, 'WW') AS week_resolved,
         TO_CHAR (resolved_date, 'Q') AS quarter_resolved,
         item,
         site,
         status,
         contact_time,
         impact_label,
         (CASE
             WHEN item = '1' THEN 'a'
             WHEN item = '2' THEN 'b'
             WHEN item = '3' THEN 'c'
             ELSE NULL
          END)
            AS new_column_name
    FROM mytable
   WHERE create_date BETWEEN TO_DATE ('2013/03/01', 'YYYY/MM/DD')
                         AND TO_DATE ('2015/08/06', 'YYYY/MM/DD')
ORDER BY item, site, create_date;

【讨论】:

    【解决方案2】:

    我猜你想要

        select
           create_date
          ,resolved_date
          ,to_char(create_date, 'YYYY') as year_create
          ,to_char(create_date, 'MM') as month_create
          ,to_char(create_date, 'WW') as week_create
          ,to_char(create_date,'Q') as quarter_create
          ,to_char(resolved_date, 'YYYY') as year_resolved
          ,to_char(resolved_date, 'MM') as month_resolved
          ,to_char(resolved_date, 'WW') as week_resolved
          ,to_char(resolved_date,'Q') as quarter_resolved
          ,item
          ,site
          ,status
          ,contact_time
          ,impact_label
          ,(case item 
            when '1' then 'a'
            when '2' then 'b'
            when '3' then 'c'
            else null 
             end) as new_column_name 
      from mytable
     where create_date between to_date('2013/03/01','YYYY/MM/DD') 
                          and to_date('2015/08/06','YYYY/MM/DD')
     order by item, site, create_date;
    

    我删除了group by 子句,因为您没有进行任何类型的聚合。由于您显然希望 case 语句更改 item 列的显示方式,因此我将其移至 select 列表。

    如果这不是您想要的,那么如果您编辑您的问题以包含一个测试用例,将会非常有帮助。向我们展示表定义,向我们展示用于插入几行的 DML,向我们展示您想要的结果,并向我们展示您获得的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 2014-09-17
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多