Skip to content

Instantly share code, notes, and snippets.

@hugotkk
Last active January 31, 2021 03:48
Show Gist options
  • Save hugotkk/e7d376b6b40359d304c1b07087bdd111 to your computer and use it in GitHub Desktop.
Save hugotkk/e7d376b6b40359d304c1b07087bdd111 to your computer and use it in GitHub Desktop.
Write a SQL query to count the number of unique user per day who logged in from both iPhone and web
create table web (
ts timestamp,
userid int,
web_sessionid int
);
create table iphone (
ts timestamp,
userid int,
iphone_sessionid int
);
insert into web (ts, userid, web_sessionid)
values
(now(), 1, 100),
(now() - interval 1 hour, 1, 101),
(now() - interval 1 day, 2, 103);
insert into iphone (ts, userid, iphone_sessionid)
values
(now(), 1, 100),
(now() - interval 2 hour, 1, 101),
(now() - interval 1 day, 3, 103);
SELECT
date(combined.ts) AS day,
count(distinct combined.userid) AS num_users
FROM
((select * from web) UNION (select * from iphone)) as combined
group by
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment