SQL:按连续记录分组第 2 部分
SQL: Group By on Consecutive Records Part 2
问题 1
我有以下 table 每隔 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。
我想 return 按照下面的示例按增量分组的聚合。
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
但是我不想使用增量(计算和填充它需要付出努力);相反,我想知道是否有任何方法可以将其计算为部分 / 而 运行 查询。
我也不介意使用视图。
问题 2
我有以下 table 以 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
我想return按人物和地点分组的汇总。
例如
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)需要做哪些修改?
听起来像是分析函数的例子。您需要将 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
问题 1
我有以下 table 每隔 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。 我想 return 按照下面的示例按增量分组的聚合。
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
但是我不想使用增量(计算和填充它需要付出努力);相反,我想知道是否有任何方法可以将其计算为部分 / 而 运行 查询。
我也不介意使用视图。
问题 2
我有以下 table 以 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
我想return按人物和地点分组的汇总。 例如
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)需要做哪些修改?
听起来像是分析函数的例子。您需要将 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