SQL识别云ID

时间:2017-02-15 14:32:51

标签: sql sql-server

我正在分析数据集。表有3列:

-CLOUD_ID (ID Field) example: 121312

-CURRENT_Action (Textfield) example: Started

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

我需要识别没有Current_Action"已删除"的Cloud_ID。作为最小MIN_Startdate。

2 个答案:

答案 0 :(得分: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,但可以很容易地将其包含在逻辑中。

答案 1 :(得分:0)

一种略有不同的方法,请比较一下:

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'