T-SQL Group By union?每组具有独特的步骤ID

时间:2018-02-26 12:26:53

标签: sql-server database

我正在尝试为每个步骤名称组创建唯一的步骤ID,如果步骤名称组重复,则会获得一个新的数字。

Step Name   Process Time                        Step ID     Step Name   Process Time            
----------  -----------------------             ----------  ----------  -----------------------
Prep        2018-02-26 12:00:00.000             1           Prep        2018-02-26 12:00:00.000
Prep        2018-02-26 12:00:10.000             1           Prep        2018-02-26 12:00:10.000
Wash        2018-02-26 12:00:20.000             2           Wash        2018-02-26 12:00:20.000
Wash        2018-02-26 12:00:30.000             2           Wash        2018-02-26 12:00:30.000
Dry         2018-02-26 12:00:40.000             3           Dry         2018-02-26 12:00:40.000
Dry         2018-02-26 12:00:50.000             3           Dry         2018-02-26 12:00:50.000
Wash        2018-02-26 12:01:00.000             4           Wash        2018-02-26 12:01:00.000
Wash        2018-02-26 12:01:10.000     ===>    4           Wash        2018-02-26 12:01:10.000
Break       2018-02-26 12:01:20.000             5           Break       2018-02-26 12:01:20.000
Prep        2018-02-26 12:01:30.000             6           Prep        2018-02-26 12:01:30.000
Wash        2018-02-26 12:01:40.000             7           Wash        2018-02-26 12:01:40.000
Dry         2018-02-26 12:01:50.000             8           Dry         2018-02-26 12:01:50.000
Dry         2018-02-26 12:02:00.000             8           Dry         2018-02-26 12:02:00.000
Dry         2018-02-26 12:02:10.000             8           Dry         2018-02-26 12:02:10.000
Dry         2018-02-26 12:02:20.000             8           Dry         2018-02-26 12:02:20.000
Finish      2018-02-26 12:02:30.000             9           Finish      2018-02-26 12:02:30.000

表记录是动态生成的,无法知道用户输入文本的步骤名称。

我在考虑使用:

select 
    CASE WHEN LAG([Step Name]) OVER (ORDER BY [Process Time]) = [Step Name]
         THEN 'var'
          ELSE 'var+1'
    END as [Step ID]

,*
from #example_1 ORDER BY [Process Time] ASC;

给我一​​些类似的东西,

Step ID     Step Name   Process Time            
----------  ----------  -----------------------
int+1       Prep        2018-02-26 12:00:00.000
int         Prep        2018-02-26 12:00:10.000
int+1       Wash        2018-02-26 12:00:20.000
int         Wash        2018-02-26 12:00:30.000
int+1       Dry         2018-02-26 12:00:40.000
int         Dry         2018-02-26 12:00:50.000
int+1       Wash        2018-02-26 12:01:00.000
int         Wash        2018-02-26 12:01:10.000
int+1       Break       2018-02-26 12:01:20.000
int+1       Prep        2018-02-26 12:01:30.000
int+1       Wash        2018-02-26 12:01:40.000
int+1       Dry         2018-02-26 12:01:50.000
int         Dry         2018-02-26 12:02:00.000
int         Dry         2018-02-26 12:02:10.000
int         Dry         2018-02-26 12:02:20.000
int+1       Finish      2018-02-26 12:02:30.000

但我不确定如何在一个选择中实际+1一个var,因为它本身不是一个循环.. 内置的组功能没有产生结果我很满意所以我有点卡住。 有什么想法吗?

0 个答案:

没有答案