SQL - 按组选择具有最大时间戳的行

SQL - Selecting the rows with max timestamp by group

我有这个table

SITE   S_ID   BAN   COUNT   P       V   TIMESTAMP
23     1       4    1500    0,05    50  10/05/17 09:58:22,609000000
23     3       3    800     0,05    50  10/05/17 09:58:22,736000000
23     2       3    3000    0,05    50  10/05/17 09:58:22,674000000
23     1       4    1500    0,05    50  10/05/17 15:57:04,079000000
23     1       4    1499    0,05    50  10/05/17 15:53:38,851000000
20     1       3    2000    0,1     50  10/05/17 10:57:07,172000000
20     2       3    2000    0,1     50  10/05/17 10:59:50,127000000
20     3       2    3000    0,1     50  10/05/17 11:00:39,051000000
20     4       2    3000    0,1     50  10/05/17 11:01:15,533000000

我正试图获得这样的东西:

SITE   S_ID   BAN   COUNT   P       V   TIMESTAMP
23     3       3    800     0,05    50  10/05/17 09:58:22,736000000
23     2       3    3000    0,05    50  10/05/17 09:58:22,674000000
23     1       4    1500    0,05    50  10/05/17 15:57:04,079000000
20     1       3    2000    0,1     50  10/05/17 10:57:07,172000000
20     2       3    2000    0,1     50  10/05/17 10:59:50,127000000
20     3       2    3000    0,1     50  10/05/17 11:00:39,051000000
20     4       2    3000    0,1     50  10/05/17 11:01:15,533000000

即对于每个站点,S_ID、BAN、COUNT、P、V 和 TIMESTAMP 以及 MAX(TIMESTAMP)

给你:

SELECT 
    SITE
    , S_ID
    ,BAN
    ,COUNT
    ,P
    ,V
    ,TimeStamp
FROM [Your Table Name]
INNER JOIN 
(
    SELECT 
        SITE
        , S_ID
        , MAX(TIMESTAMP) as MaxTimeStamp 
    FROM [Your Table Name] 
    GROUP BY 
        SITE 
        , S_ID
    ) AS MaxDAata ON
        MaxData.SITE = [Your Table Name].SITE 
        AND MaxData.S_ID = [Your Table Name].S_ID
       AND MaxData.TimeStamp = [Your Table Name].MaxTimeStamp 

我会使用分析函数对组(分区)中的日期进行排名,然后仅 return 排名靠前的项目。我不确定您的分区,所以我使用了字段 site, s_id, ban

select * from 
    (with test_data (SITE,   S_ID,   BAN,   s_COUNT,   P,       V,   s_TIMESTAMP) as (
    select 23,     1,       4,    1500,    0.05,    50,  to_timestamp('10/05/17 09:58:22,609','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 23,     3,       3,    800 ,    0.05,    50,  to_timestamp('10/05/17 09:58:22,736','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 23,     2,       3,    3000,    0.05,    50,  to_timestamp('10/05/17 09:58:22,674','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 23,     1,       4,    1500,    0.05,    50,  to_timestamp('10/05/17 15:57:04,079','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 23,     1,       4,    1499,    0.05,    50,  to_timestamp('10/05/17 15:53:38,851','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 20,     1,       3,    2000,    0.1 ,    50,  to_timestamp('10/05/17 10:57:07,172','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 20,     2,       3,    2000,    0.1 ,    50,  to_timestamp('10/05/17 10:59:50,127','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 20,     3,       2,    3000,    0.1 ,    50,  to_timestamp('10/05/17 11:00:39,051','MM/DD/YY HH24:MI:SS,FF') from dual union all
    select 20,     4,       2,    3000,    0.1 ,    50,  to_timestamp('10/05/17 11:01:15,533','MM/DD/YY HH24:MI:SS,FF') from dual
    )
    SELECT site, s_id, ban, s_count, p, v, s_timestamp, 
           rank() over (partition by site, s_id, ban order by s_timestamp desc) as r
    FROM test_data
    )
where r = 1
order by site, s_id


SITE|S_ID|BAN|S_COUNT|P|V|S_TIMESTAMP|R
20|1|3|2000|0.1|50|05-OCT-17 10.57.07.172000000 AM|1
20|2|3|2000|0.1|50|05-OCT-17 10.59.50.127000000 AM|1
20|3|2|3000|0.1|50|05-OCT-17 11.00.39.051000000 AM|1
20|4|2|3000|0.1|50|05-OCT-17 11.01.15.533000000 AM|1
23|1|4|1500|0.05|50|05-OCT-17 03.57.04.079000000 PM|1
23|2|3|3000|0.05|50|05-OCT-17 09.58.22.674000000 AM|1
23|3|3|800|0.05|50|05-OCT-17 09.58.22.736000000 AM|1