2015-11-04 23 views
0

(背景:我試圖在一系列cameraapis中查找活動的「高峯」小時,定義爲開始和結束日期在1之間的項目最多(從小時開始)例如,1:00到2:00在該時間範圍內可能有8個條目,但2:00到3:00有12個條目 - 所以我想讓它返回12輸入時間範圍)。爲PostgreSQL的組中的某個選擇獲取對應的值

我無法從組的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,但我無法弄清楚要將關聯的start_hour選擇到該max_entries。因爲它是一個組,所以它需要聚合函數,而實際上我並不需要這些函數。有什麼建議?

+0

你有沒有考慮把這部分到紅寶石? –

+0

你的問題需要更多的上下文,現在解碼有點困難。但是爲什麼不在你的select語句中使用聚合函數:array_agg(expression)來提取你需要的id? – charlysisto

回答

0

didnt很明白的問題...使用窗口功能

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