如何基于选择查询来自定义列输出

时间:2018-10-26 13:28:37

标签: sql oracle

需要帮助。

我在一个表中有3列,例如created_dt,event_dt和amount。

dest test_event;
==================
created_dt timestamp,
event_dt timestamp,
amount number

样本记录应该是

EVENT_DT    created_dt amount
==============================
 01-11-2018 10-10-2018  100
02-09-2018  10-08-2018  200
22-08-2018  20-09-2018  200

我需要以下格式的输出

EVENT_DT    created_dt  before amount   After amount
=====================================================
01-11-2018  10-10-2018  NA                100
02-09-2018  10-08-2018  NA                200
22-08-2018  20-09-2018  200               NA

条件是,如果event_dt大于created dt,则应将其显示为金额之后;如果event_dt小于created_Dt,则应将其显示为金额之前。

1 个答案:

答案 0 :(得分:0)

您可以在select语句中使用CASE;

select EVENT_DT,created_dt,
case when EVENT_DT > created_dt then cast(amount as varchar(20)) else 'NA' end as before_amount,
case when EVENT_DT < created_dt then cast(amount as varchar(20)) else 'NA' end as after_amount
from table