Last active
December 1, 2021 08:56
-
-
Save Mr--John-Doe/cff2d32c8288f97b1671c229e7645b66 to your computer and use it in GitHub Desktop.
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 | |
LEFT JOIN SCHEMA.tableX X ON X.product = a.product | |
LEFT JOIN SCHEMA.tableY Y ON Y.product = a.product AND Y.name IN ('Wagyu Beef', 'Ribs') | |
LEFT JOIN SCHEMA.tableZ Z ON Z.product = a.product AND Z.name = 'Pork Ribs' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment