1

说我有价值观:

Reference      Class  Timestamp
XXHAG70        11     2013-05-07 14:29:59.820
XXHAG70        11     2013-05-07 14:33:19.780
XXHAG70        17     2013-05-07 14:30:19.930
XXHAG70        17     2013-05-07 14:33:44.690
PAF7010        06     2008-11-06 10:25:07.140
PAF7010        06     2009-02-27 12:56:11.420

每个类都有一个重复值,因此是成对的。我只想为每个参考中的每个类选择最旧的时间戳。

4

4 回答 4

1

您可以使用 row_number 函数。

SELECT  Reference ,
        Class ,
        Timestamp
FROM    ( SELECT    Reference ,
                    Class ,
                    Timestamp ,
                    ROW_NUMBER() OVER ( PARTITION BY Reference, Class ORDER BY Timestamp) AS rnum
          FROM      MyTable
        ) A
WHERE   rnum = 1;
于 2013-06-24T14:41:20.840 回答
0

你可以使用MIN

SELECT MIN(TimeStamp)
FROM YourTable
WHERE Class IN (SELECT DISTINCT Class From YourTable)
于 2013-06-24T14:36:28.690 回答
0

要获取每个类/参考的最旧版本,请使用MINand GROUP BY

SELECT Reference, Class, MIN(Timestamp)
FROM myTable
GROUP BY Reference, Class
于 2013-06-24T14:39:00.337 回答
0

这将删除除最旧的行之外的所有行(reference, class)

delete  from t1
from    (
        select  row_number() over (
                    partition by reference, class 
                    order by Timestamp desc) rn
        ,       *
        from    YourTable
        ) t1
where   t1.rn > 1

SQL Fiddle 上的实时示例。

于 2013-06-24T14:42:39.017 回答