Notes:
This presentation does not contain sensitive business information at Autodesk, but contains practices.
I hope you and your families are well during these times! Moreover, I encourage y’all to Social Distance, Wear Masks, and keep an eye out to social movements during these next critical months.
Scenario:
-- Past 7 Days of Myspace User data.
select
id, createdDate, events.login_id, geoState, firstName, lastName, sum(login.logIn) total_logins
from mySpace_events as events
LEFT OUTER JOIN mySpace_users users
on users.id = events.user_id
WHERE login.date >= current_date - (select min(createdDate) from mySpace_users where profile = 'Musician')
group by 1,2,3,4,5,6
It may turn out that you’re writing style may not be up-to-par with the team’s standards.
And that’s not your fault! It’s something you should not be ashamed about, but understanding about.
But...
Saved for Reserved Words & Functions
SELECT
id, createdDate, events.login_id, geoState, firstName, lastName, SUM(login.logIn) total_logins
FROM mySpace_events AS events
LEFT OUTER JOIN mySpace_users users ON users.id = events.user_id
WHERE login.date >= CURRENT_DATE - (SELECT MIN(createdDate) FROM mySpace_users WHERE profile = 'Musician')
GROUP BY 1,2,3,4,5,6
SELECT
id,
createdDate,
events.login_id,
geoState,
firstName,
lastName,
SUM(login.logIn) total_logins
FROM mySpace_events AS events
LEFT OUTER JOIN mySpace_users users ON
users.id = events.user_id
WHERE login.date >= CURRENT_DATE - (SELECT MIN(createdDate) FROM mySpace_users WHERE profile = 'Musician')
GROUP BY 1,2,3,4,5,6
SELECT
id,
createdDate,
events.login_id,
geoState,
firstName,
lastName,
SUM(login.logIn) total_logins
FROM mySpace_events AS events
LEFT OUTER JOIN mySpace_users users
ON users.id = events.user_id
WHERE login.date >= CURRENT_DATE - (
SELECT
MIN(createdDate)
FROM mySpace_users
WHERE profile = 'Musician'
)
GROUP BY 1,2,3,4,5,6
WITH earliest_musician_user_date AS (
SELECT
MIN(createdDate)
FROM mySpace_users
WHERE profile = 'Musician'
)
SELECT
id,
createdDate,
events.login_id,
geoState,
firstName,
lastName,
SUM(login.logIn) total_logins
FROM mySpace_events AS events
LEFT OUTER JOIN mySpace_users users ON
users.id = events.user_id
WHERE login.date >= CURRENT_DATE - earliest_musician_user_date
GROUP BY 1,2,3,4,5,6
WITH earliest_musician_user_date AS (
SELECT
MIN(created_date)
FROM mySpace_users
WHERE profile = 'Musician'
)
SELECT
id,
created_date,
events.login_id,
geo_state,
first_name,
last_name,
SUM(login.logIn) AS total_logins
FROM mySpace_events AS events
LEFT OUTER JOIN AS myspace_users users
ON users.id = events.user_id
WHERE login.date >= CURRENT_DATE - earliest_musician_user_date
GROUP BY 1,2,3,4,5,6
We learned by encouraging a readability practice for submitting code to a team’s repository, production, developers do not have to face:
And possible enable:
Never RIGHT, move it to the LEFT..and SQL ON…
Unofficial: My own past experience on SQL Readability form 2018 learnings
Note: Company never open-sourced Readability standards for SQL, but has for others