(背景:我试图在一系列cameraapis中找到"峰值"活动小时,定义为具有开始和结束日期在1小时之间的最多条目(开始)小时的开头)例如,1:00到2:00可能在该时间范围内有8个条目,但是2:00到3:00有12个条目 - 所以我希望让它返回12个条目的时间范围。 )
我无法从组的SELECT查询中获取关联数据。这是代码:
def reach_peak_hour_by_date_range(start_date, end_date)
placement_self_device_id = self.device_id
query = <<-SQL
SELECT max(y.num_entries) AS max_entries
FROM
(
SELECT x.starting_hour, count(*) AS num_entries
FROM
(
SELECT date_trunc('hour', visitor_start_time) starting_hour
FROM Cameraapis WHERE device_id = '#{placement_self_device_id}'::text AND visitor_start_time > '#{start_date}'::timestamp AND visitor_end_time < '#{end_date}'::timestamp
) AS x
GROUP BY x.starting_hour
) AS y
SQL
results = Placement.connection.execute(query)
binding.pry
end
Cameraapi在代码中引用了device_id,visitor_start_time和visitor_end_time。
此代码在1小时内成功返回max_entries,但我无法确定要将相关的starting_hour与该max_entries相关联的SELECT。因为它是一个组,所以它需要聚合功能,而我实际上并不需要这些功能。有什么建议吗?
答案 0 :(得分:0)
没有完全理解这个问题......使用窗口函数
select starting_hour , num_entries from (
SELECT starting_hour ,y.num_entries, max(y.num_entries) over() AS max_entries
FROM
(
SELECT x.starting_hour, count(*) AS num_entries
FROM
(
SELECT date_trunc('hour', visitor_start_time) starting_hour
FROM Cameraapis WHERE device_id = '#{placement_self_device_id}'::text AND visitor_start_time > '#{start_date}'::timestamp AND visitor_end_time < '#{end_date}'::timestamp
) AS x
GROUP BY x.starting_hour
) AS y
) as u
where num_entries = max_entries
此查询返回与高峰时段相关的所有条目,您可以修改它以仅返回条目计数,并使用相关小时选择小时和使用不同或分组计数
select * from
(
select x.*, max(num_entries) over()as max_num_entries from
(
SELECT Cameraapis.* ,date_trunc('hour', visitor_start_time) as starting_hour, count(*) over( partition by date_trunc('hour', visitor_start_time)) as num_entries
FROM Cameraapis WHERE device_id = '#{placement_self_device_id}'::text AND visitor_start_time > '#{start_date}'::timestamp AND visitor_end_time < '#{end_date}'::timestamp
) as x
) as x where max_num_entries = num_entries