Oracle:当一列的值更改时选择行

问题描述 投票:1回答:2

我有下表:

 PLACE       USER_ID Date
---------- ---------- -----------------------------
        ABC      4     14/04/20 12:05:29,255000000  
        ABC      4     14/04/20 15:42:28,389000000  
        ABC      4     14/04/20 18:33:20,202000000  
        ABC      4     14/04/20 22:51:28,339000000    
        XYZ      4     14/04/20 11:07:23,335000000     
        XYZ      2     14/04/20 12:15:12,123000000    
        ABC      4     13/04/20 22:09:33,255000000   
        QWE      4     13/04/20 10:18:29,144000000 
        XYZ      2     14/04/20 10:05:47,255000000   

并且当我选择的user_id的地点按日期更改顺序时,我需要获取行。所以期望的结果应该是这个(对于user_id 4):

 PLACE       USER_ID           DATE
---------- ---------- -----------------------------
        ABC      4     14/04/20 12:05:29,255000000     
        XYZ      4     14/04/20 11:07:23,335000000 
        ABC      4     13/04/20 22:09:33,255000000    
        QWE      4     13/04/20 10:18:29,144000000 

我尝试使用最小日期,但是在我的示例中,如果用户返回该位置,我会丢失数据:

 SELECT MIN(DATE), PLACE FROM user_places WHERE USER_ID=4 GROUP BY PLACE

我得到的结果(缺少一行):

 PLACE       USER_ID           DATE
---------- ---------- -----------------------------
        XYZ      4     14/04/20 11:07:23,335000000 
        ABC      4     13/04/20 22:09:33,255000000    
        QWE      4     13/04/20 10:18:29,144000000 

提前感谢!

sql oracle date gaps-and-islands
2个回答
1
投票

在Oracle 12.1和更高版本中,对于match_recognize子句来说,像这样的空缺问题是一件容易的事。例如:

表格设置

alter session set nls_timestamp_format = 'dd/mm/rr hh24:mi:ss,ff';

create table user_places (place, user_id, date_) as 
  select 'ABC', 4, to_timestamp('14/04/20 12:05:29,255000000') from dual union all  
  select 'ABC', 4, to_timestamp('14/04/20 15:42:28,389000000') from dual union all  
  select 'ABC', 4, to_timestamp('14/04/20 18:33:20,202000000') from dual union all
  select 'ABC', 4, to_timestamp('14/04/20 22:51:28,339000000') from dual union all
  select 'XYZ', 4, to_timestamp('14/04/20 11:07:23,335000000') from dual union all
  select 'XYZ', 2, to_timestamp('14/04/20 12:15:12,123000000') from dual union all
  select 'ABC', 4, to_timestamp('13/04/20 22:09:33,255000000') from dual union all
  select 'QWE', 4, to_timestamp('13/04/20 10:18:29,144000000') from dual union all
  select 'XYZ', 2, to_timestamp('14/04/20 10:05:47,255000000') from dual
;

commit;

查询和输出

select place, user_id, date_
from   (select * from user_places where user_id = 4)
match_recognize (
  partition by user_id
  order by date_
  all rows per match
  pattern (a {- b* -} )
  define  b as place = a.place
)
order by date_ desc   --   if needed
;

PLACE  USER_ID  DATE_
-----  -------  ---------------------------
ABC          4  14/04/20 12:05:29,255000000
XYZ          4  14/04/20 11:07:23,335000000
ABC          4  13/04/20 22:09:33,255000000
QWE          4  13/04/20 10:18:29,144000000

这里要注意的几件事:

  • DATE是保留关键字。列名不好。我使用了DATE_代替;请注意结尾的下划线。
  • 我将值4硬编码。当然,更好的做法是将其变成bind variable
  • [如果您真的一次只需要一次user_id,则执行我所做的工作效率最高-在子查询中首先过滤行。但是,如果您需要针对同一查询中的所有用户ID进行此操作,则不需要子查询;您可以从表本身中进行选择,并且需要在partition by user_id子句顶部的match_recognize之前添加order by date_

0
投票

您可以在子查询中使用lag()来检索“上一个”位置,然后在上一个位置与当前位置不同的行上进行过滤:

select place, user_id, date
from (
    select t.*, lag(place) over(partition by user_id order by date) lag_place
    from mytable t
) t
where lag_place is null or place <> lag_place

这将为您提供所有用户的预期输出。如果只需要用户4,则可以在子查询中进行过滤(并且不需要partition by用户):

select place, user_id, date
from (
    select t.*, lag(place) over(order by date) lag_place
    from mytable t
    where user_id = 4
) t
where lag_place is null or place <> lag_place
© www.soinside.com 2019 - 2024. All rights reserved.