Skip to content

Instantly share code, notes, and snippets.

@cdussud
cdussud / numbers_table.sql
Created September 30, 2021 13:31
Generate a sequence of numbers
-- Generates a numbers table
-- Works on Azure SQL, BigQuery, Postgres, Redshift, and Snowflake
--
-- empty_rows generates 36 rows with a single column
-- the FROM statements are cross joins -- each multiplies the number of rows by 36
-- row_number gives us our desired output
-- going all the way to e is 36^5 = 60M rows
WITH empty_rows AS (
SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL SELECT 1 AS n UNION ALL
-- DateDiff function that returns the difference between two timestamps in the given date_part (weeks, months, etc) as an integer
-- This behaves like the DateDiff function in warehouses like Redshift and Snowflake, which count the boundaries between date_parts
CREATE OR REPLACE FUNCTION datediff (date_part VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
RETURNS INT AS $diff$
DECLARE
years INT = 0;
days INT = 0;
hours INT = 0;
minutes INT = 0;
-- Generates timestamps that follow a function: y = r^x
with
quickly_increasing_timestamps as (
select
'2020-01-01'::timestamp as time_start,
sysdate as time_end,
date_diff('minute', time_start, time_end) as time_span_minutes,
random() as x, -- generate a random value from 0 to 1 for x
@cdussud
cdussud / increasing_timestamps.sql
Last active February 17, 2021 20:42
Generates timestamps that increase following an exponential function
-- Generates timestamps that follow a function: y = r^x
with
increasing_timestamps as (
select
'2020-01-01'::timestamp as time_start,
sysdate as time_end,
date_diff('minute', time_start, time_end) as time_span_minutes,
random() as x, -- generate a random value from 0 to 1 for x
@cdussud
cdussud / random_timestamps.sql
Last active February 15, 2021 21:48
Random timestamps between two intervals in Redshift
-- generates timestamps randomly between the start and end times
with
random_timestamps as (
select
-- some 'variables' just to make things clear
'2021-01-01'::timestamp as time_start,
sysdate as time_end,
date_diff('minute', time_start, time_end) as time_span_minutes,
@cdussud
cdussud / redshift_cross_numbers.sql
Created January 29, 2021 19:48
How to generate a numbers table in Redshift
-- Generates a list of sequential integers
-- How it works:
-- empty_rows is 36 rows of 1
-- cross join with itself any number of times as needed
-- a -> 36 rows
-- a, b -> 36*36 rows
-- a, b, c -> 36*36*36 rows
-- row_number gives us our integer for the table
@cdussud
cdussud / numbers-azure.sql
Created January 18, 2021 19:54
Generate a sequence of numbers in Azure SQL
-- 10M numbers for Azure SQL Database
WITH digit (d) AS
(
select 0 as d union all
select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9
),
seq (num) as (
select top 1000000000000 -- top is needed for order by
@cdussud
cdussud / numbers-redshift.sql
Created January 18, 2021 19:20
Sequence of numbers in Redshift
-- generate 10M numbers on Redshift
with digit as (
select 0 as d union all
select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9
),
seq as (
select a.d + (10 * b.d) + (100 * c.d) + (1000 * d.d) + (10000 * e.d) + (100000 * f.d) + (1000000 * g.d) as num
from digit a
/Users/Cedric/Code/SocialWeb/app/services/events/events.slim:
9 |{{rsvpLanguage}}
10 .rsvp.styleLargeBC ng-if="!event.isPublic && !$root.user.isLoggedIn()"
11: a.anywhereCTA.styleBadass href="https://signup.wework.com" target="_blank"
12 |Become a member and gain access to this event and hundreds more like it.
13 .nonMemberAttendanceBlock ng-if="!$root.user.isLoggedIn() && event.isPublic"
/Users/Cedric/Code/SocialWeb/app/services/login/login.slim:
31 .not-member.styleLargeBC
32 | Not a member yet?