First row for each group
<p>I have table containing row for each visit to an endpoint. Table looks like this:</p>

<pre><code>user_id STRING
endpoint_id STRING
created_at TIMESTAMP
</code></pre>

<p>Example data:</p>

<pre><code>user-1, endpoint-1, 2016-01-01 01:01:01 UTC
user-2, endpoint-1, 2016-01-01 01:01:01 UTC
user-1, endpoint-2, 2016-01-02 01:01:01 UTC
user-1, endpoint-1, 2016-01-02 01:01:01 UTC
user-1, endpoint-1, 2016-01-03 01:01:01 UTC
</code></pre>

<p><strong>How to get first visit row for each user and resource.</strong></p>

<p>What is the best way to construct such query?</p>

<p>Expected result:</p>

<pre><code>user-1, endpoint-1, 2016-01-01 01:01:01 UTC
user-2, endpoint-1, 2016-01-01 01:01:01 UTC
user-1, endpoint-2, 2016-01-02 01:01:01 UTC
</code></pre>

<hr>

<p>Here's what I came up with, but this query will not work for big amount of data. I use window function to group together repeaing user/resource rows:</p>

<pre><code>SELECT
    user_id,
    endpoint_id,
    created_at
FROM (
    SELECT 
        poll_id, 
        endpoint_id, 
        created_at,
        FIRST_VALUE(created_at) OVER (PARTITION BY user_id, endpoint_id ORDER BY created_at DESC) AS first_created_at
    FROM 
        [visits]
    )
WHERE
    created_at = first_created_at
</code></pre>