2016-11-21 32 views
0

想象一下,我有一個與Redshift表格相似的結構。 Product_Bill_ID是此表的主鍵。紅移計數與變量

| Store_ID | Product_Bill_ID | Payment_Date  
| 1  | 1    | 01/10/2016 11:49:33  
| 1  | 2    | 01/10/2016 12:38:56  
| 1  | 3    | 01/10/2016 12:55:02  
| 2  | 4    | 01/10/2016 16:25:05  
| 2  | 5    | 02/10/2016 08:02:28  
| 3  | 6    | 03/10/2016 02:32:09 

如果我想查詢Product_Bill_ID的數量,一個商店在第一個小時賣出後,出售了其第一Product_Bill_ID,我怎麼能做到這一點?

這個例子應該結局

| Store_ID | First_Payment_Date | Sold_First_Hour  
| 1  | 01/10/2016 11:49:33 | 2     
| 2  | 01/10/2016 16:25:05 | 1      
| 3  | 03/10/2016 02:32:09 | 1     

回答

0

你需要得到的第一個小時。這是很容易的利用窗口函數:

select s.*, 
     min(payment_date) over (partition by store_id) as first_payment_date 
    from sales s 

然後,你需要做的日期過濾和聚集:

select store_id, count(*) 
from (select s.*, 
      min(payment_date) over (partition by store_id) as first_payment_date 
     from sales s 
    ) s 
where payment_date <= first_payment_date + interval '1 hour' 
group by store_id; 
0
SELECT 
    store_id, 
    first_payment_date, 
    SUM(
     CASE WHEN payment_date < DATEADD(hour, 1, first_payment_date) THEN 1 END 
    ) AS sold_first_hour 
FROM 
(
    SELECT 
     *, 
     MIN(payment_date) OVER (PARTITION BY store_id) AS first_payment_date 
    FROM 
     yourtable 
) 
    parsed_table 
GROUP BY 
    store_id, 
    first_payment_date