Skip to content

Instantly share code, notes, and snippets.

@sbealer
Created November 1, 2016 20:43
Show Gist options
  • Save sbealer/436de9a7d3605e4972148bc4e7cbd4c6 to your computer and use it in GitHub Desktop.
Save sbealer/436de9a7d3605e4972148bc4e7cbd4c6 to your computer and use it in GitHub Desktop.
Redshift Time Dimension SQL
CREATE VIEW dw.dim_time_vw AS
with nums AS (
SELECT TOP 86400
row_number() over (
PARTITION BY NULL ORDER BY id) AS num
FROM l_browser)
SELECT
to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'HH24MISS') AS time_key,
to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'HH24:MI:SS') AS time,
cast(to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'HH24') AS int) AS hour,
cast(to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'MI') AS int) AS minute,
cast(to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'SS') AS int) AS second
FROM nums;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment