我有一个查询,其中我有一个单位的电灯开关打开的时间,开关(开始)时间和开关(结束)时间。
我想知道有多少个电灯开关开着,有人知道我该怎么做吗?
查询示例:
Select Flat, ID_light, start, end, 1 as ON
from lightsI/O示例:
输入数据:
Flat Id Light Start End On
1 1 01/01/2021 00:00:15 01/01/2021 00:59:00 1
1 2 01/01/2021 00:00:15 01/01/2021 01:59:00 1
2 1 01/01/2021 00:00:15 01/01/2021 00:01:15 1
2 1 01/01/2021 00:02:00 01/01/2021 01:00:00 1
2 2 01/01/2021 00:00:00 01/01/2021 08:00:00 1文本输出:
Flat Start End Lights On
1 01/01/2021 00:00:15 01/01/2021 00:59:00 2
1 01/01/2021 00:59:00 01/01/2021 01:59:00 1
2 01/01/2021 00:00:00 01/01/2021 00:00:15 1
2 01/01/2021 00:00:15 01/01/2021 00:01:15 2
2 01/01/2021 00:01:15 01/01/2021 00:02:00 1
2 01/01/2021 00:02:00 01/01/2021 01:00:00 2
2 01/01/2021 01:00:00 01/01/2021 08:00:00 1发布于 2021-01-06 02:19:18
range数据类型简化了这一过程。
获取所有单位和时间:
with times as (
select flat, start as ts from lights
union
select flat, end_ from lights
),使用lead()为每个间隔构造行
all_ivals as (
select flat, ts as start,
lead(ts) over (partition by flat
order by ts) as end_
from times
)使用范围重叠&&运算符连接回原始表并进行聚合:
select a.flat, a.start, a.end_, count(*) as lights_on
from all_ivals a
join lights l
on tsrange(l.start, l.end_, '[)') && tsrange(a.start, a.end_, '[)')
and l.flat = a.flat
where a.end_ is not null
group by a.flat, a.start, a.end_
order by a.flat, a.start
;结果:
flat start end_ lights_on
1 2021-01-01 00:00:15 2021-01-01 00:59:00 2
1 2021-01-01 00:59:00 2021-01-01 01:59:00 1
2 2021-01-01 00:00:00 2021-01-01 00:00:15 1
2 2021-01-01 00:00:15 2021-01-01 00:01:15 2
2 2021-01-01 00:01:15 2021-01-01 00:02:00 1
2 2021-01-01 00:02:00 2021-01-01 01:00:00 2
2 2021-01-01 01:00:00 2021-01-01 08:00:00 1
7 rowshttps://stackoverflow.com/questions/65577212
复制相似问题