分区后从每个组中获取特定值

时间:2016-05-31 11:02:48

标签: sql oracle

我有下表 -

ID | Type | Val0 | Val1 | Group | Creation_date
1  |  A   |  V1  | Val1 | G1    | 24sep, 2011
2  |  A   |  V1  | Val2 | G1    | 27oct, 2008
3  |  A   |  V2  | Val3 | G2    | 19oct, 2003
4  |  B   |  X1  | Xal1 | G3    | 15 dec, 2001
5  |  B   |  X2  | Xal2 | G4    | 15 dec, 2000
6  |  B   |  X2  | Xal3 | G4    | 15 dec, 1990
7  |  C   |  X3  | Xal4 | G5    | 23Dec, 2001

我想只保留特定类型的最新值,并提取旧的但不属于与最新项目关联的组的值。因此,对于上表,我想要提取这些项目 - Val3,Xal2,Xal3,我可以在以后删除。

由于A的最新值为Val1,属于G1组,B的最新值为Xal1,C的最新值为Xal4。

有人可以建议我如何使用SQL来做到这一点吗?

2 个答案:

答案 0 :(得分:2)

您可以使用row_number()。不需要聚合:

select t.*
from (select t.*,
             row_number() over (partition by t.type order by creation_date desc) as seqnum
      from t
     ) t
where seqnum = 1;

但是,您需要不在此组中的值。也就是说,您希望val1seqnum不是1的值。这将是:

select t.val1
from (select t.*, min(seqnum) over (partition by t.type, t.group) as min_seqnum -- DON'T USE group FOR COLUMN NAMES!
      from (select t.*,
                   row_number() over (partition by t.type order by creation_date desc) as seqnum
            from t
           ) t
     ) t
where min_seqnum <> 1;

答案 1 :(得分:1)

您可以使用ROW_NUMBER()

SELECT p.type,p.val1 FROM (
    SELECT t.*,
           ROW_NUMBER() OVER(PARTITION BY t.type ORDER BY t.max_d DESC) as rnk
    FROM (SELECT s.Type,s.Val1,MAX(s.creation_date) as max_d
          FROM YourTable s
          GROUP BY s.type,s.val1) t) p
WHERE p.rnk > 1