2017-02-20 63 views
2

行后直接我有以下的列SQL服务器:只有行过滤包含特定文本

application_uuid 
changed_at_utc 
changed_by 
name 

我谨application_uuidchanged_at_utc排序表。然后,我要筛选只排在那里application_status具有文本“准备打分”后直接过来的行

Python和熊猫,我会做这样的事情...

application_statuses = application_statuses.sort_values(['application_uuid', 'changed_at_utc'], ascending=[True, True]).reset_index(drop=True) 
indexes = application_statuses[application_statuses['application_status']=='Ready for Scoring'].index + 1 
next_statuses = application_statuses.ix[indexes] 

我怎样才能使用SQL做同样的事情?

+0

您能否显示一些示例数据和预期结果? –

回答

3

根据您的解释,您可以使用lead函数来执行此操作。

select next_application_status,application_uuid,changed_at_utc,changed_by 
from (select t.*, 
     lead(application_status) over(order by application_uuid,changed_at_utc) as next_appliaction_status 
     from tablename t 
    ) t1 
where application_status = 'Ready for Scoring' 

如果这对每个application_uuid工作要做,包括在lead象下面这样partition by

select next_application_status,application_uuid,changed_at_utc,changed_by 
from (select t.*, 
     lead(application_status) over(partition by application_uuid order by changed_at_utc) as next_appliaction_status 
     from tablename t 
    ) t1 
where application_status = 'Ready for Scoring' 

如果application_status Ready for Scoring后需要所有行,获取特定行的时间戳和选择所有其他时间戳这是更大的。这假定一个application_uuid最多只有一行Ready for Scoring状态。

select application_status,application_uuid,changed_at_utc,changed_by 
from (select t.*, 
     max(case when application_status='Ready for Scoring' then changed_at_utc end) over(partition by application_uuid) as status_time 
     from tablename t 
    ) t1 
where changed_at_utc > status_time 
+0

非常感谢!这正是我所期待的 –