在'select'中合并oracle中的两个日期列并在合并列上排序

时间:2012-11-14 00:30:54

标签: oracle

您好我有一个选择查询,我从表中选择2个不同的日期列,我需要将它们合并到结果中的一列,并按日期的降序排序。 查询如下所示:

select work_order_no,install_remove_type,removed_item_id,installed_item_id,
       removed_performed_on as irdate, 
       installed_performed_on as irdate 
  from install_remove_items 
 where work_order_no is not null  
   and (installed_item_id = 'IID000174096' or removed_item_id= 'IID000174096')

上述查询的结果是创建了2列

irdate - for removed_performed_on

irdate_1 - 对于installed_performed_on

这不是我想要的方式。如何在“irdate”列中提供与日期相关的数据,并按照desc顺序排序?

更新

install_remove_items表中的示例数据(INSTALL_REMOVE_TYPE字段中4种操作类型的4条记录):

 ir_id: xxxxyyyy5555,  install_remove_type= 'INSTALL', workOrderno = 22335522, installed_item_id = IID000174096, installed_performed_on = '11/10/2012', removed_item_id = null, removed_performed_on = null.

 ir_id: xxxxyyyy3333, install_remove_type= 'REMOVE', workOrderno = 223542, installed_item_id = null, installed_performed_on = null, removed_item_id = IID000174096, removed_performed_on = '11/12/2012'.

 ir_id: xxxxyyyy1111,  install_remove_type= 'WORKEDON', workOrderno = 111111, installed_item_id = null, installed_performed_on = null, removed_item_id = IID000174096, removed_performed_on = '11/13/2012'.

 ir_id: xxxxyyyy2222, install_remove_type= 'REPLACED', workOrderno = 444444, installed_item_id = IID000174096, installed_performed_on = 11/15/2012, removed_item_id = IID000174096, removed_performed_on = '11/14/2012'.

因此有4种类型的Action,“删除”字段用于REMOVE操作,“安装”字段用于INSTALL操作,两个字段用于REPLACED操作,“已删除”字段用于WORKEDON操作。

我想编写一个查询,根据install_performed_on和removed_performed_on日期(并获得该日期)查找最后执行的四种操作(与今天的日期或输入日期值相比)。

希望这能使事情变得清晰。

我知道union可能很好,但是目前我们用于SQL的Java API不支持union,所以我可能需要组合SQL和JAVA代码来实现我的输出。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果没有您想要的样本数据,它并不完全清楚您想要的是什么。但这会是吗?

select *
  from (select work_order_no,install_remove_type,installed_item_id,
                   installed_performed_on as irdate 
              from install_remove_items 
             where work_order_no is not null  
               and installed_item_id = 'IID000174096'
            union all
            select work_order_no,install_remove_type,removed_item_id,
                   removed_performed_on as irdate
              from install_remove_items 
             where work_order_no is not null  
               and removed_item_id= 'IID000174096')
         order by irdate desc;

根据您的样本进行修订:

SQL> select *
  2    from (select workOrderno, install_remove_type,
  3                 case
  4                   when installed_performed_on > removed_performed_on
  5                     or removed_performed_on is null then ir.installed_performed_on
  6                   else removed_performed_on
  7                 end last_action_date,
  8                 coalesce(ir.installed_item_id,ir.removed_item_id)  item_id
  9            from install_remove_items ir
 10           where workOrderno is not null
 11             and (installed_item_id = 'IID000174096' or removed_item_id= 'IID000174096')
 12           order by last_action_date desc )
 13   where rownum = 1
 14  /

WORKORDERNO INSTALL_RE LAST_ACTI ITEM_ID
----------- ---------- --------- --------------------
     444444 REPLACED   15-NOV-12 IID000174096

或您可以执行的每个项目的列表:

SQL> select * from install_remove_items;

IR_ID                INSTALL_RE WORKORDERNO INSTALLED_ITEM_ID    INSTALLED REMOVED_ITEM_ID      REMOVED_P
-------------------- ---------- ----------- -------------------- --------- -------------------- ---------
xxxxyyyy5555         INSTALL       22335522 IID000174096         10-NOV-12
xxxxyyyy3333         REMOVE          223542                                IID000174096         12-NOV-12
xxxxyyyy1111         WORKEDON        111111                                IID000174096         13-NOV-12
xxxxyyyy2222         REPLACED        444444 IID000174096         15-NOV-12 IID000174096         14-NOV-12
3242                 REMOVE              43                                IID000174097         14-DEC-12
123                  INSTALL            123 IID000174097         15-NOV-12

6 rows selected.

SQL> select workorderno, install_remove_type, item_id, last_action_date
  2    from (select l.*,
  3                  row_number() over(partition by item_id order by last_action_date desc) rn
  4             from (select workorderno, install_remove_type,
  5                           case
  6                             when installed_performed_on > removed_performed_on or
  7                                  removed_performed_on is null then
  8                              ir.installed_performed_on
  9                             else
 10                              removed_performed_on
 11                           end last_action_date,
 12                           coalesce(ir.installed_item_id, ir.removed_item_id) item_id
 13                      from install_remove_items ir
 14                     where workorderno is not null) l)
 15   where rn = 1;

WORKORDERNO INSTALL_RE ITEM_ID              LAST_ACTI
----------- ---------- -------------------- ---------
     444444 REPLACED   IID000174096         15-NOV-12
         43 REMOVE     IID000174097         14-DEC-12