所以这就是情况 - 我们的记录使用名称/日期的复合键。我希望看到连续执行了2个特定操作的所有名称,所以我想做类似
的操作 select name from
(select name as n, date as d from table where action='action1'),
(select name from table where name = n and date > d and action='action2' and rownum=1 order by date desc)
但它将n和d计为无效标识符。我怎样才能做到我需要的呢?
答案 0 :(得分:1)
一种方法可能是:
SELECT DISTINCT
如果单个名称的每个操作可以有多个实例,则可能会给您重复。在这种情况下,使用{{1}}来消除重复可能就足够了。
请注意,这并不意味着这两个动作一个接一个地发生,只是动作2发生在动作1之后的某个时间。
答案 1 :(得分:1)
分析函数非常适合这类事情....免责声明这是快速而肮脏的,列名称有点误导。 LAG / LEAD是你想要玩的选项
http://sqlfiddle.com/#!4/bd7b2/7
select name,thedate,theaction,prev_action,prev_date from
(
select name,thedate,theaction,
lag(theaction) over (partition by name order by thedate,theaction) as prev_action,
lag(thedate) over (partition by name order by thedate,theaction) as prev_date
from table1
order by name,thedate,theaction
)
where theaction = 'action1' and prev_action = 'action2'
;