我想select
包含每个equipment_id
的最小日期时间值或最大日期时间值的所有行,并持续一段时间(每天)。
下面的代码在整个期间内为每个min
选择max
和equipment_id
日期时间值。我可以/应该改变什么以达到期望的选择结果?
注意:代码段中用大括号括起来的值表示动态值。
select *
from equipment
where created_at in (select min(created_at)
from equipment
where created_at >= {start_datetime} and created_at < {end_datetime}
group by equipment_id
) or
created_at in (select max(created_at)
from equipment
where created_at >= {start_datetime} and created_at < {end_datetime}
group by equipment_id
)
order by account, equipment_id, created_at asc;
答案 0 :(得分:1)
使用窗口功能:
select e.*
from (select e.*,
min(e.created_at) over (partition by e.equipment_id, to_date(e.created_at)) as min_ca,
max(e.created_at) over (partition by e.equipment_id, to_date(e.created_at)) as max_ca
from equipment e
) e
where e.created_at in (min_ca, max_ca)
order by account, equipment_id, created_at asc;
答案 1 :(得分:0)
使用to_date
从datetime值中提取日期。然后,加入汇总查询:
with agg as
(select to_date(created_at) as created_date,
equipment_id,
min(created_at) as min_created_at,
max(created_at) as max_created_at
from equipment
group by to_date(created_at),
equipment_id
)
select e.*
from equipment e
left join agg
on e.equipment_id = agg.equipment_id
and to_date(e.created_at) = agg.created_date
where e.created_at in (agg.min_created_at,
agg.max_created_at)