简体   繁体   中英

SQL: Group By on Consecutive Records Part 2

Problem 1

I have the following table which shows the location of a person at 1 hour intervals

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

The delta increments by +1 every time the location changes. I would like to return an aggregate grouped by delta as per example below.

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

I am using the following query which was taken and adapted from here SQL: Group By on Consecutive Records (which works fine):

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

However I would like to not use the delta (it takes effort to calculate and populate it); instead I am wondering if there is any way to calculate it as part / whilst running the query.

I wouldn't mind using a view either.

Problem 2

I have the following table which shows the location of different people at 1 hour intervals

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

I would like to return an aggregate grouped by person and location. For example

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

What modifications do I need to the above query (Problem 1)?

Sounds like a case for analytic functions. You would need to add 1 hour to the EndTime, but that will be dependent on the data type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM