简体   繁体   English

根据列条件从2个值中选择包含多行的第一个值,否则从所有行选择第二个值ORACLE

[英]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 . 因此, descorder 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.

相关问题 根据第二列的公共数据检索第一列的行 - retrieve rows of 1st column based on common data on 2nd column 如果第 2 个表中存在第 1 个表的值,则用第 1 个和第 2 个表的总和更新第 2 个表的值,否则将第一个表数据插入到第 2 个表 - If value of 1 table exists in the 2nd table, update the 2nd table value with sum of 1st and 2nd tables, else insert 1st table data to 2nd table T-SQL从第一个选择的值在第二个选择联合的每一行 - T-SQL Value from 1st select on each row in 2nd select of a union all SQL Server:从同一列中具有匹配值的第一行之后的列中选择所有行 - SQL Server : select all rows from a column after the 1st row with matching value from the same column 如何选择不同的行,其中一列可能具有许多相同的值,但所有第二列均具有相同的值? - How do I select distinct rows where a column may have a number of the same values but all their 2nd columns have the same value? 根据列条件从两个值中选择第一个值及其相关行,否则选择第二个值及其相关行 - Select first value and its related rows out of two values based on column condition otherwise select second value and its related rows SQL ORDER BY 第一列,但如果第一列为空,则用第二列中的值替换空值 - SQL ORDER BY 1st column, but if 1st column is empty replace the empty value by a value from 2nd column 在Access中为每个人选择第1行,第2行和第3行 - Select 1st, 2nd, and 3rd rows for each person in Access 仅当所有匹配第一个where子句的行也匹配第二个时返回结果 - Only return results if all rows that match 1st where clause also match the 2nd 从 SQL Server 表中的带连字符的字符串列中提取第 1 个第 2 个第 3 个值直到第 n 个值 - Extract 1st 2nd 3rd values till nth value from hyphenated string column in SQL Server table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM