2017-02-20 51 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

非常感謝!這正是我所期待的 –

相關問題