我有以下数据
PET_REF XDATE TYPE
123 01/01/2017 OBJ
123 01/01/2017 OBJ
123 01/01/2017 OBJ
123 02/01/2017 LVE
456 01/01/2017 OBJ
456 01/01/2017 LVE
456 02/01/2017 OBJ
是否可以仅返回最新(按XDATE)TYPE不是LVE的PET_REF行
因此,对于上面的数据,输出应为
PET_REF XDATE TYPE
456 01/01/2017 OBJ
456 01/01/2017 LVE
456 02/01/2017 OBJ
答案 0 :(得分:2)
使用FIRST_VALUE
分析函数
Select * from
(
select PET_REF, XDATE, TYPE, First_Value(TYPE)over(Partition by PET_REF order by XDATE desc) as Latest_Type
from yourtable
)a
Where Latest_Type <> 'LVE'
答案 1 :(得分:0)
解决此问题的一种方法是尝试将它们放在子查询中。
SELECT *
FROM t
WHERE c1 IN (
SELECT c1
FROM t
WHERE (c1,c2) IN (SELECT c1, MAX(c2)
FROM t
GROUP BY 1)
AND c3 <> 'LVE');
答案 2 :(得分:0)
这是一个选项:
SQL> with test (pet_ref, xdate, type) as
2 (select 123, date '2017-01-01', 'obj' from dual union all
3 select 123, date '2017-01-01', 'obj' from dual union all
4 select 123, date '2017-01-01', 'obj' from dual union all
5 select 123, date '2017-01-02', 'lve' from dual union all --
6 select 456, date '2017-01-01', 'obj' from dual union all
7 select 456, date '2017-01-01', 'lve' from dual union all --
8 select 456, date '2017-01-02', 'obj' from dual
9 ),
10 inter as
11 (select pet_ref, type,
12 rank() over (partition by pet_ref order by xdate desc) rnk
13 from test
14 )
15 select * from test t
16 where t.pet_ref not in (select i.pet_ref from inter i
17 where i.rnk = 1
18 and i.type = 'lve');
PET_REF XDATE TYP
---------- ---------- ---
456 02/01/2017 obj
456 01/01/2017 lve
456 01/01/2017 obj
SQL>
答案 3 :(得分:0)
通过以下方式使用订单更容易实现此目的:
SELECT *
FROM datatable
WHERE PET_REF LIKE (SELECT MAX(PET_REF) FROM datatable)
ORDER BY XDATE ASC, TYPE DESC;