Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Last active December 23, 2015 06:29
Show Gist options
  • Save randyzwitch/6594458 to your computer and use it in GitHub Desktop.
Save randyzwitch/6594458 to your computer and use it in GitHub Desktop.
Example of SQL formatting
#Capitalize reserved keywords, indent between keywords and for multi-line statements
#Tables on separate lines
#Untested, hopefully this is valid SQL :)
SELECT
a.key,
a.col2,
a.col3,
COALESCE(a.col4,0) AS col4,
b.col5,
b.col6,
totals.tot_items,
totals.tot_cost
FROM accounts AS a
LEFT JOIN bills AS b ON (a.key = b.key)
LEFT JOIN (SELECT
col2,
SUM(items) AS tot_items,
SUM(cost) AS tot_cost
FROM items
GROUP BY col2) AS totals ON (a.col2 = totals.col2)
WHERE a.key in (SELECT key FROM active_accounts WHERE date_utc >= '2013-09-01')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment