We take a holistic approach towards best practices at 173Tech. Data and analytics are here to guide us in our daily decisions. It needs to be clearly defined, trusted, and easily digestible for everyone.
We build quality and thoughtfulness into all aspects of our processes. It is the key towards a successful data strategy.
In this post, we would like to share our SQL formatting guidelines. We standardise because:
WITH registration AS
(SELECT ...
FROM ...)
, activity AS
(SELECT ...
FROM ...)
SELECT ...
FROM registration
On long lines (eg: CASE WHEN) use a logical point for wrapping the line manually. Some options:
a) Align the WHEN keywords
SELECT CASE WHEN t.column = 'a very long value' THEN 1
WHEN t.column = 'another very long value' THEN 2
END AS column_alias
b) Split on operator
SELECT first_picking_cost_usd +
extra_picking_cost_usd * (n_items - 1) +
packaging_cost_usd +
shipping_cost_usd AS fulfilment_cost_usd
FROM orders
c) One line per parameter to a function
SELECT COALESCE(t.a_very_looooooooooooooooooong_value,
t.another_very_looooooooooooooooooong_value)
AS long_values
By subscribing, you agree to receive a monthly newsletter from 173Tech.