Question

I have one table on Redshift and in a query I'm trying to have the list of ids and the list of ids from a sequence generated.

WITH 

missing_rows AS (
  SELECT id, created_at
  FROM logs
  WHERE created_at > GETDATE() - INTERVAL '2 hours'
),

range AS (
  SELECT min(id) AS min_id, max(id) AS max_id
  FROM missing_rows
),

series AS (
  SELECT (row_number() over (ORDER BY 1)) + (range.min_id - 1) AS id, 
  FROM logs
  FULL OUTER JOIN range ON ???? -- here is my issue
  WHERE active_connections.id 
BETWEEN range.min_id -- how to use min_id from range
    AND range.max_id -- how to use max_id from range 
)

SELECT *
FROM series

How to use range values inside the next query series?

Was it helpful?

Solution

Range returns exactly 1 row, so you can use a cartesian product (cross join):

, series AS (
    SELECT (row_number() over (ORDER BY 1)) + (range.min_id - 1) AS id, 
    FROM logs
    CROSS JOIN range
    WHERE active_connections.id BETWEEN range.min_id
                                    AND range.max_id
)

I'm not familiar with redshift so if cross join is not supported you can use a join where the join predicate always evaluates to true:

    FROM logs
    JOIN range ON 1=1
    WHERE active_connections.id BETWEEN range.min_id
                                    AND range.max_id

or an old style "," join;

    FROM logs, range
    WHERE active_connections.id BETWEEN range.min_id
                                    AND range.max_id

You can also use a normal join and use the where predicate in the on clause:

    FROM logs
    JOIN range 
        ON active_connections.id BETWEEN range.min_id
                                     AND range.max_id
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top