[英]select 1st value with many rows out of 2 values based on column condition else 2nd value all rows ORACLE
i have below oracle sql 我下面有oracle sql
select * from a
where a.description in
('Rebook All (1)', 'Created stock identifier')
and a.date between '03/18/2017 00:00:01'
AND '03/25/2017 00:00:00'
and a.unit = '38240007294677'
Now result can have both description like below for a Unit number or can have 1 of the description , when it has both description for a Unit number i need to select only 'Rebook all (1)'(this description can be there many times as shows below in results) description not 'Created stock identifier' and its related details from table a and when it has only 1 description out of these 2 then if 'Rebook All (1)' not there select 'Created stock identifier' and vice versa. 现在结果既可以具有如下所示的单位编号描述,也可以具有1的描述,当它具有单位编号的两个描述时,我只需要选择“全部重新预订(1)”(此描述可以有很多次在结果中显示如下),说明不是“创建的库存标识符”及其相关详细信息,来自表a,并且在这2个中只有1个描述时,如果没有“全部重新预订(1)”,则选择“创建的库存标识符”,反之亦然。 how do i select it? 我该如何选择? Thanks in advance. 提前致谢。
Date Description
3/20/2017 10:11:12.769000 AM Created stock identifier 3/20/2017 10:11:12.769000 AM创建了库存标识符
3/20/2017 10:10:47.775000 AM Created stock identifier 3/20/2017 10:10:47.775000 AM创建库存标识符
3/20/2017 10:10:47.782000 AM Created stock identifier 3/20/2017 10:10:47.782000 AM创建库存标识符
3/20/2017 8:14:41.696000 PM Rebook all (1) 2017/3/20下午8:14:41.696000重新预订全部(1)
3/20/2017 8:14:41.885000 PM Rebook all (1) 2017/3/20下午8:14:41.885000重新预订全部(1)
3/20/2017 8:13:51.622000 PM Rebook all (1) 2017/3/20下午8:13:51.622000重新预订全部(1)
One method uses rank()
: 一种方法使用rank()
:
select a.*
from (select a.*,
rank() over (partition by unit order by description desc) as seqnum
from a
where a.description in ('Rebook All (1)', 'Created stock identifier')
a.date >= date '2017-03-18' and
a.date < date '2017-03-25' and
a.unit = '38240007294677'
) a
where seqnum = 1;
Notes: - This uses the fact that "Rebook" comes alphabetically after "Created". 注意:-使用“重新预订”在“创建”之后按字母顺序排列的事实。 Hence the desc
in the order by
. 因此, desc
按order by
。 - I prefer ANSI standard date formats. -我更喜欢ANSI标准日期格式。 - Notice the change to the date logic, to eliminate the need for times on the dates. -注意日期逻辑的更改,以消除对日期的时间需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.