Oracle:根据给定日期选择行

时间:2019-01-30 22:21:56

标签: sql oracle

我有以下数据:

WITH  T AS (
SELECT 333 "ID",TO_DATE('2015/04/01','yyyy/mm/dd') "DT",331 AS VAL1,'GG' AS VAL2 FROM DUAL
UNION ALL 
SELECT 444 "ID",TO_DATE('2012/05/10','yyyy/mm/dd') "DT",441 AS VAL1,'AA' AS VAL2 FROM DUAL
UNION ALL 
SELECT 444 "ID",TO_DATE('2013/03/01','yyyy/mm/dd') "DT",442 AS VAL1,'AA1' AS VAL2 FROM DUAL
UNION ALL 
SELECT 444 "ID",TO_DATE('2018/08/12','yyyy/mm/dd') "DT",443 AS VAL1,'AA1' AS VAL2 FROM DUAL
UNION ALL
SELECT 555 "ID",TO_DATE('2012/05/10','yyyy/mm/dd') "DT",551 AS VAL1,'AA' AS VAL2 FROM DUAL
UNION ALL 
SELECT 555 "ID",TO_DATE('2017/03/01','yyyy/mm/dd') "DT",552 AS VAL1,'BB1' AS VAL2 FROM DUAL
UNION ALL 
SELECT 555 "ID",TO_DATE('2018/03/01','yyyy/mm/dd') "DT",553 AS VAL1,'GGGA1' AS VAL2 FROM DUAL
UNION ALL
SELECT 555 "ID",TO_DATE('2018/09/12','yyyy/mm/dd') "DT",554 AS VAL1,'JJBB1' AS VAL2 FROM DUAL
UNION ALL 
SELECT 555 "ID",TO_DATE('2019/10/09','yyyy/mm/dd') "DT",555 AS VAL1,'OOOUA1' AS VAL2 FROM DUAL
UNION ALL 
SELECT 666 "ID",TO_DATE('2012/05/10','yyyy/mm/dd') "DT",441 AS VAL1,'AA' AS VAL2 
FROM DUAL
UNION ALL 
SELECT 666 "ID",TO_DATE('2013/03/01','yyyy/mm/dd') "DT",442 AS VAL1,'AA1' AS VAL2 
FROM DUAL
UNION ALL 
SELECT 666 "ID",TO_DATE('2016/08/12','yyyy/mm/dd') "DT",443 AS VAL1,'AA1' AS VAL2 
FROM DUAL
)
SELECT * FROM (
SELECT id,val1 ,val2,dt
      ,ROW_NUMBER() OVER(PARTITION BY id ORDER BY dt ASC) rnk
  FROM T
);

        ID       VAL1 VAL2   DT               RNK
---------- ---------- ------ --------- ----------
       333        331 GG     01-APR-15          1
       444        441 AA     10-MAY-12          1
       444        442 AA1    01-MAR-13          2
       444        443 AA1    12-AUG-18          3
       555        551 AA     10-MAY-12          1
       555        552 BB1    01-MAR-17          2
       555        553 GGGA1  01-MAR-18          3
       555        554 JJBB1  12-SEP-18          4
       555        555 OOOUA1 09-OCT-19          5   

在这里,我需要根据输入日期选择记录。

例如: INPUT_DATE ='2016年12月17日'

场景:

  1. 如果一个ID只有一个记录,请选择一个记录-简单
  2. 如果一个ID记录多,则选择“ INPUT_DATE”大于“ DT”列的记录,然后选择“最小日期”

预期输出:

        ID       VAL1 VAL2   DT               RNK
---------- ---------- ------ --------- ----------
       333        331 GG     01-APR-15          1
       444        443 AA1    12-AUG-18          3
       555        552 BB1    01-MAR-17          2

3 个答案:

答案 0 :(得分:2)

您会这样做:

select t.*
from (select t.*,
             row_number() over (partition by id,
                                order by (case when dt >= INPUT_DT then 1 else 2 end), dt asc
                               ) as seqnum
      from t
     ) t
where seqnum = 1;

答案 1 :(得分:1)

以下查询对两种情况均适用。

版本号返回2条记录的ID具有不唯一的dt。

SELECT * FROM t MyT
WHERE dt = (
    SELECT MIN(DT) FROM t
    WHERE id = t.id AND dt >= INPUT_DATE
)

将始终返回1条记录的版本(它选择保留的记录是“随机”)

SELECT *
FROM (
    SELECT *, row_number() OVER (PARTITION BY id ORDER BY dt) AS MyRank FROM T
    WHERE dt >= INPUT_DATE
) MyT
WHERE MyRank = 1

通过采用第二个查询并将row_number()更改为rank(),还可以得到与第一个查询相同的结果。这实际上与您当时喜欢的语法有关。

答案 2 :(得分:0)

我没有oracle数据库,因此这不是确切的代码,但是您可以尝试以下类似方法。

 select * from test where val1 in(
 select val1 from test  where dt in 
 (select max(DT) from test where DT>INPUT_DATE group by val1))