Oracle-按记录范围的最小值分组

时间:2019-03-19 08:12:17

标签: sql oracle oracle12c

举例来说,假设我在表中具有以下值:

---------------------------------------
|    ID_USER    |     START_DATE      |
---------------------------------------
|       1       | 01/01/2018 08:00:00 |
|       1       | 01/01/2018 08:15:00 |
|       2       | 01/01/2018 08:30:00 |
|       1       | 01/01/2018 08:45:00 |
|       1       | 01/01/2018 09:00:00 |
|       2       | 01/01/2018 09:15:00 |
|       2       | 01/01/2018 09:30:00 |
|       1       | 01/01/2018 09:45:00 |
---------------------------------------

现在,我想按ID_USER分组,选择最小的START_DATE值,只要它们是相关的即可。解决方案是:

---------------------------------------
|    ID_USER    |     START_DATE      |
---------------------------------------
|       1       | 01/01/2018 08:00:00 |
|       2       | 01/01/2018 08:30:00 |
|       1       | 01/01/2018 08:45:00 |
|       2       | 01/01/2018 09:15:00 |
|       1       | 01/01/2018 09:45:00 |
---------------------------------------

您知道如何进行此查询吗?

3 个答案:

答案 0 :(得分:1)

使用LAG比较当前行和上一行:

with cte as 
 (
   select  ID_USER, START_DATE,
      lag(ID_USER, 1, -1) over (order by START_DATE) as prev_user
   from myTable
 )
select *
from cte
where ID_USER <>  prev_user

答案 1 :(得分:1)

with s (id_user, start_date) as (
select 1, to_date('01/01/2018 08:00:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 08:15:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 08:30:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 08:45:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 09:00:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 09:15:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 09:30:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 09:45:00', 'dd.mm.yyyy hh24:mi:ss') from dual)
select id_user, start_date
from
   (select s.*, lag(id_user) over (order by start_date) prev_user
    from s
   )
where lnnvl(prev_user = id_user);

   ID_USER START_DATE
---------- -------------------
         1 2018-01-01 08:00:00
         2 2018-01-01 08:30:00
         1 2018-01-01 08:45:00
         2 2018-01-01 09:15:00
         1 2018-01-01 09:45:00

答案 2 :(得分:0)

-- Oracle 12c+: Pattern matching
with s (id_user, start_date) as (
select 1, to_date('01/01/2018 08:00:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 08:15:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 08:30:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 08:45:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 09:00:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 09:15:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 2, to_date('01/01/2018 09:30:00', 'dd.mm.yyyy hh24:mi:ss') from dual union all
select 1, to_date('01/01/2018 09:45:00', 'dd.mm.yyyy hh24:mi:ss') from dual)
select id_user, start_date
from s
match_recognize(
order by start_date
measures
   id_user    as id_user,
   start_date as start_date
pattern (v+)
define v as id_user = first(id_user)
);

   ID_USER START_DATE
---------- -------------------
         1 2018-01-01 08:15:00
         2 2018-01-01 08:30:00
         1 2018-01-01 09:00:00
         2 2018-01-01 09:30:00
         1 2018-01-01 09:45:00