如果日期列大于sysdate,则'Y' [英] case when date column is greater than sysdate then 'Y'

查看:69
本文介绍了如果日期列大于sysdate,则'Y'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一条CASE语句,该语句检查另一列中的日期,并以别名结束。

我需要创建另一个case语句,该语句将查看新的别名列,并查看它是否大于sysdate。如果是,则为"Y"。

以下是当前查询:

select
  v.voyage "Voyage"
  ,v.service "Service"
  ,to_char(vp.eta_date, 'MONTH dd, yyyy') "ETA"
  ,case 
    when v.service = "USA" then to_char(vp.eta_date - 2, 'MONTH dd, yyyy')
    else 'n/a'
    end as "Notice"
from 
  table

产生以下结果:

Voyage | Service | ETA   | Notice
_______________________________
test12 | USA     | 12/13 | 12/11
test14 | USA     | 12/15 | 12/13

我需要这样做:

select
  v.voyage "Voyage"
  ,v.service "Service"
  ,to_char(vp.eta_date, 'MONTH dd, yyyy') "ETA"
  ,case 
    when v.service = 'USA' then to_char(vp.eta_date - 2, 'MONTH dd, yyyy')
    else 'n/a'
    end as "Notice"
  ,case 
    when "Notice" > sysdate then 'Y' else 'N'
    end as "Sent"
from 
  table   

应生成以下内容:

Voyage | Service | ETA   | Notice | Sent
________________________________________
test12 | USA     | 12/13 | 12/11  | N
test14 | USA     | 12/15 | 12/13  | Y

但我收到一个错误,内容为:

a non-numeric character was found where a numeric was expected

如何修复此问题?

*编辑*

我收到以下错误:

"Notice": invalid identifier

推荐答案

您需要其中一个复制第二个case中的&Quot;Notify&Quot;列的整个定义-这不是最佳做法,也不是DRY(即复制相同的代码)

  ,case 
    when /*"Notice"*/
     case 
     when v.service = 'USA' then to_char(vp.eta_date - 2, 'MONTH dd, yyyy')
     else 'n/a'
     end 
     > sysdate then 'Y' else 'N'
    end as "Sent"

最好使用子查询或CTE

with dt as (
select
  v.voyage "Voyage"
  ,v.service "Service"
  ,to_char(vp.eta_date, 'MONTH dd, yyyy') "ETA"
  ,case 
    when v.service = 'USA' then to_char(vp.eta_date - 2, 'MONTH dd, yyyy')
    else 'n/a'
    end as "Notice"
from 
  table  
)
select dt.* ,
case 
    when "Notice" > sysdate then 'Y' else 'N'
    end as "Sent"
from dt

正如注释中指出的,这将使您能够编译(分析)查询,但是不会返回预期结果

这篇关于如果日期列大于sysdate,则'Y'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆