2017-02-15 73 views
0

我正在分析一個數據集。表有3列:SQL識別雲ID的

-CLOUD_ID (ID Field) example: 121312 

-CURRENT_Action (Textfield) example: Started 

-MIN_STARTDATE (Date) example: 2016-04-20 17:03:58.633 

我需要確定Cloud_ID的不具備Current_Action「已刪除」作爲最低MIN_Startdate。

+0

不會這項工作:'SELECT * FROM表,其中current_Action <>'deleted'' – TheGameiswar

+0

嗨thegameiswar,沒有因爲我需要的關係到最小值(日期)。每個Cloud-ID都有幾個代理,我需要確定那些沒有刪除的代理。 – Joe

+0

oh ok.Thanks.adding more sample data helps.Can你也可以看看這個鏈接來改進未來的問題:https://spaghettidba.com/2015/04/24/how-to-post-at-sql-question -on-a-public-forum/ – TheGameiswar

回答

0

一個sligtly不同的方法,請比較PERF:

with x as (select *, row_number() over(partition by cloud_id order by min_startdate) rn from @t) 
select cloud_id from x where rn = 1 and current_action <> 'deleted' 
+0

也做了詭計!相同的速度 – Joe

2

您可以使用窗口函數:

select distinct cloud_id 
from (select t.*, 
      min(min_startdate) over (partition by cloud_id) as min_min_startdate 
     from t 
    ) t 
where min_startdate = min_min_startdate and 
     Current_Action <> 'Deleted'; 

注:這是假定Current_Action不是NULL,但很容易被包含在邏輯。

+0

擊敗我幾秒鐘;) –

+0

嗨Gordon Linoff,thx爲您的快速回復。這一個工作,給了我預期的結果! THX爲您提供幫助。 JOE – Joe