Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logic improvement #2

Open
mike-hosseini opened this issue Oct 27, 2019 · 0 comments
Open

Logic improvement #2

mike-hosseini opened this issue Oct 27, 2019 · 0 comments

Comments

@mike-hosseini
Copy link

On question 3 of Text Confirmation problem, your join predicate ignores the fact that a user can send confirmation on both the first day of sign up and second day of sign up.

-- dialect: MySQL

SELECT
  e.user_id
FROM Email AS e
JOIN Text AS t
ON e.user_id = t.user_id
  AND DATEDIFF(t.ts, e.ts) = 1
WHERE t.action = 'CONFIRMED';

A more accurate answer would be to only get the first date the user sent a confirmation and then perform a join and do a date comparison.

-- dialect: PostgreSQL

with first_confirm as (
  select
        user_id,
        ts,
        row_number() over (partition by user_id order by ts) as rn
  from text
  where action = 'CONFIRMED'
) select e.user_id
from email e
  join first_confirm f on e.user_id = f.user_id
  where f.rn = 1 and cast(f.ts as date) -  cast(e.ts as date) = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant