SQL:连续记录分组第2部分

时间:2015-04-04 22:06:04

标签: sql sql-server grouping

问题1

我有下表,以1小时为间隔显示一个人的位置

Id  EntityID    EntityName  LocationID          Timex   delta
1   1           Mickey      Club house          0300    1
2   1           Mickey      Club house          0400    1
3   1           Mickey      Park                0500    2
4   1           Mickey      Minnies Boutique    0600    3
5   1           Mickey      Minnies Boutique    0700    3
6   1           Mickey      Club house          0800    4
7   1           Mickey      Club house          0900    4
8   1           Mickey      Park                1000    5
9   1           Mickey      Club house          1100    6

每次位置更改时,增量都会增加+1。 我想返回一个按delta分组的聚合,如下例所示。

EntityName  LocationID          StartTime   EndTime
Mickey      Club house          0300        0500
Mickey      Park                0500        0600
Mickey      Minnies Boutique    0600        0800
Mickey      Club house          0800        1000
Mickey      Park                1000        1100
Mickey      Club house          1100        1200

我正在使用以下查询,这是从这里采取和改编的 SQL: Group By on Consecutive Records (工作正常):

select 
    min(timex) as start_date
    ,end_date
    ,entityid
    ,entityname
    ,locationid
    ,delta
from 
    (
    select 
        s1.timex
        ,(  
         select 
            max(timex)
         from 
            [locationreport2] s2
         where 
            s2.entityid = s1.entityid
            and s2.delta = s1.delta
            and not exists 
                (
                select 
                    null
                from 
                    [dbo].[locationreport2] s3
                where 
                    s3.timex < s2.timex
                    and s3.timex > s1.timex
                    and s3.entityid <> s1.entityid
                    and s3.entityname <> s1.entityname
                    and s3.delta <> s1.delta
                )
            ) as end_date
     , s1.entityid
     , s1.entityname
     , s1.locationid
     , s1.delta
from 
    [dbo].[locationreport2] s1
) Result

group by 
    end_date
    , entityid
    , entityname
    , locationid
    , delta
order by 
    1 asc

但是我想不使用delta(需要花费精力来计算和填充它);相反,我想知道是否有任何方法可以在运行查询时将其计算为部分。

我不介意使用视图。

问题2

我有下表,以1小时的间隔显示不同人的位置

Id  EntityID    EntityName  LocationID  Timex   Delta
1   1           Mickey      Club house  0900    1
2   1           Mickey      Club house  1000    1
3   1           Mickey      Park        1100    2
4   2           Donald      Club house  0900    1
5   2           Donald      Park        1000    2
6   2           Donald      Park        1100    2
7   3           Goofy       Park        0900    1
8   3           Goofy       Club house  1000    2
9   3           Goofy       Park        1100    3

我想返回按人员和位置分组的聚合。 例如

EntityID    EntityName  LocationID  StartTime   EndTime
1           Mickey      Club house  0900    1100
1           Mickey      Park        1100    1200
2           Donald      Club house  0900    1000
2           Donald      Park        1000    1200
3           Goofy       Park        0900    1000
3           Goofy       Club house  1000    1100
3           Goofy       Park        1100    1200

上述查询需要进行哪些修改(问题1)?

1 个答案:

答案 0 :(得分:0)

听起来像分析函数的情况。您需要向EndTime添加1小时,但这取决于数据类型。

SELECT EntityName, LocationID, StartTime, EndTime
  FROM ( SELECT EntityName, LocationID
           ,MIN(Timex) OVER (PARTITION BY EntityID, delta ORDER BY delta) AS StartTime
           ,MAX(Timex) OVER (PARTITION BY EntityID, delta ORDER BY delta) AS EndTime
       FROM locationreport2
    ) x
  GROUP BY EntityName, LocationID, StartTime, EndTime
  ORDER BY EntityName, StartTime