This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT Store | |
, Client | |
, CASE WHEN client=10 THEN 0 ELSE SALES_GBP END AS SALES | |
, ... | |
FROM SOME_TABLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT Store | |
, Client | |
, CASE | |
WHEN client=10 THEN 0 -- Exclude staff store sales | |
ELSE SALES_GBP END AS SALES | |
, ... | |
FROM SOME_TABLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FOR p_date in DATE_LIST: | |
INSERT INTO result | |
SELECT p_date, ... | |
FROM ... JOIN ... | |
WHERE dt between trunc('month', p_date) and p_date | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO result | |
SELECT p_date, ... | |
FROM ... JOIN ... | |
INNER JOIN DATE_LIST | |
ON dt between trunc('month', p_date) and p_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer