Skip to content

Instantly share code, notes, and snippets.

SELECT a.id
, coalesce(someColumn, someColumn2) someColumn
, someOtherColumn
, oneOther
, andThenSome
, plusThisONe
FROM this_important_table as a
LEFT JOIN other_table as b
on id = _id
INNER JOIN some_other_table as c
SELECT sales.id
, coalesce(client.someColumn, store.someColumn2) someColumn
, client.someOtherColumn
, store.oneOther
, store.andThenSome
, store.plusThisONe
FROM this_important_table as sales
LEFT JOIN other_table as client
on sales.id = client._id
INNER JOIN some_other_table as store
SELECT Store
, Client
, CASE WHEN client=10 THEN 0 ELSE SALES_GBP END AS SALES
, ...
FROM SOME_TABLE
SELECT Store
, Client
, CASE
WHEN client=10 THEN 0 -- Exclude staff store sales
ELSE SALES_GBP END AS SALES
, ...
FROM SOME_TABLE
@Mr--John-Doe
Mr--John-Doe / for_loops_article-code_1.sql
Created November 24, 2021 17:44
The Procedural Thinking - Cursor For Loops
FOR date IN list_of_dates
LOOP
INSERT INTO final_table(date, revenue_mtd)
SELECT @date as date, sum(revenue) as revenue_mtd
FROM sales
WHERE sales.dt between date_trunc('month',@date) and @date;
END LOOP;
@Mr--John-Doe
Mr--John-Doe / for_loops_article-code_2.sql
Created November 24, 2021 17:44
The Relational Thinking - Technique #1 - Inner Join
/* FABRICATE SOME EXAMPLES */
WITH fake_sales AS (
select '2020-12-01'::date dt, 100.00 revenue union all
select '2020-12-02'::date dt, 200.00 revenue union all
select '2020-12-08'::date dt, 300.00 revenue union all
select '2020-12-09'::date dt, 400.00 revenue union all
select '2020-12-10'::date dt, 500.00 revenue
)
, fake_dates AS (
SELECT '2020-12-01'::date + SEQ4() dt
@Mr--John-Doe
Mr--John-Doe / for_loops_article-code_3.sql
Created November 24, 2021 17:46
How To Do For Loops in SQL
FOR p_date in DATE_LIST:
INSERT INTO result
SELECT p_date, ...
FROM ... JOIN ...
WHERE dt between trunc('month', p_date) and p_date
@Mr--John-Doe
Mr--John-Doe / for_loops_article-code_4.sql
Created November 24, 2021 17:47
How To Do For Loops in SQL - 2
INSERT INTO result
SELECT p_date, ...
FROM ... JOIN ...
INNER JOIN DATE_LIST
ON dt between trunc('month', p_date) and p_date
@Mr--John-Doe
Mr--John-Doe / for_loops_article-code_5.sql
Created November 24, 2021 17:48
Relational Thinking - Technique #2 - Windowed Function
/* FABRICATE SOME EXAMPLES */
WITH fake_sales AS (
select '2020-12-01'::date dt, 100.00 revenue union all
select '2020-12-02'::date dt, 200.00 revenue union all
select '2020-12-08'::date dt, 300.00 revenue union all
select '2020-12-09'::date dt, 400.00 revenue union all
select '2020-12-10'::date dt, 500.00 revenue
)
, fake_dates AS (
SELECT '2020-12-01'::date + SEQ4() dt
DELETE FROM SCHEMA.TABLE_XYZ; INSERT INTO SCHEMA.TABLE_XYZ(sale_id, user_id, sale_value, no_of_items)
SELECT a.sale_id, a.user_id, a.sale_value
, COALESCE(CASE WHEN a.product = 310 THEN 90 WHEN a.product = 311 THEN 10
WHEN a.product = 312 THEN 50 WHEN X.type = 'Month' THEN X.quantity * 30
ELSE a.quantity END, 0) AS no_of_items FROM (SELECT DISTINCT c.product, pd.productName, c.quantity FROM (
SELECT rb.redeem_id, pd.product, COUNT(*) AS quantity
FROM tableA rb
JOIN tableB r ON r.id = rb.redeem_id
JOIN tableC pd ON pd.externalId = r.product_id AND rb.something_else = 'cooksHeaven' AND pd.is_paid_for = 1 GROUP BY 1, 2, 3) c
INNER JOIN SCHEMA.tableD pd USING (product)) a