Note that there are some explanatory texts on larger screens.

plurals
  1. POFilter on window function according to hour
    primarykey
    data
    text
    <p>I want to use two different (yet similar) window functions to calculate two values <code>SUM</code> and <code>COUNT</code> on <code>is_active over user_id+item</code>, only up to the time of the row - minus 1 hour. My intuition was use ROWS <code>UNBOUNDED PRECEDING</code> but that way I can't filter over the time</p> <pre><code>COUNT(1) OVER(PARTITION BY user_id, item ORDER BY req_time ROWS UNBOUNDED PRECEDING) SUM(is_active) OVER(PARTITION BY user-id, item ORDER BY req_time ROWS UNBOUNDED PRECEDING) </code></pre> <p>However, that doesn't take under consideration the '1 hour ago' interval factor</p> <p>Consider the following data:</p> <pre><code>user_id | req_time | item | is_active | --------+--------------------+-------------------+--- 1 | 2011-01-01 12:00:00| 1 | 0 | 1 | 2011-01-01 12:30:00| 1 | 1 | 1 | 2011-01-01 15:00:00| 1 | 1 | 1 | 2011-01-01 16:00:00| 1 | 0 | 1 | 2011-01-01 16:00:00| 2 | 0 | 1 | 2011-01-01 16:20:00| 2 | 1 | 2 | 2011-02-02 11:00:00| 1 | 1 | 2 | 2011-02-02 13:00:00| 1 | 0 | 1 | 2011-02-02 16:20:00| 1 | 0 | 1 | 2011-02-02 16:30:00| 2 | 0 | </code></pre> <p>I expect the following results: "value 1" is SUM(is_active) and "value 2" is COUNT(1):</p> <pre><code>user_id | req_time | item | value 1 | value 2 | --------+--------------------+-----------------+---------+ 1 | 2011-01-01 12:00:00| 1 | 0 | 0 | 1 | 2011-01-01 12:30:00| 1 | 0 | 0 | 1 | 2011-01-01 15:00:00| 1 | 1 | 2 | 1 | 2011-01-01 16:00:00| 1 | 2 | 3 | 1 | 2011-01-01 16:00:00| 2 | 0 | 0 | 1 | 2011-01-01 16:20:00| 2 | 0 | 0 | 2 | 2011-02-02 11:00:00| 1 | 0 | 0 | 2 | 2011-02-02 13:00:00| 1 | 1 | 1 | 1 | 2011-02-02 16:20:00| 1 | 2 | 4 | 1 | 2011-02-02 16:30:00| 2 | 1 | 2 | </code></pre> <p>I'm using Greenplum 4.21 which is based on Postgresql 8.2.15</p> <p>Thanks in advance! gilibi</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload