Here is my question
Question B: I have a log table below
Date Visitor
2020-01-01 A
2020-01-01 B
2020-01-01 C
2020-01-02 C
2020-01-02 D
2020-01-03 D
2020-01-03 E
2020-01-04 A
2020-01-04 E
2020-01-04 F
2020-01-04 D
Q_B1: find # of visitor_change (gain or loss) compared to the previous day on each day
Example: On 1/2, we had C & D, compared to A & B & C on 1/1, then # of visitor_change should be -1 (2 - 3 = -1). Similarly, on 1/3, we had D & E (2), the same as on 1/2 (C & D), then the # of visitor_change should be 0 (2 - 2 = 0)
Output:
Date #
2020-01-01 3
2020-01-02 -1 (2-3)
2020-01-03 0 (2-2)
2020-01-04 2 (4-2)
Q_B2: find # of total_new_visitors (who never visited before) on each day
Example: On 1/1, A & B & C were all net new, so the number should be 3. On 1/2, we had net new visitor D, so the number should be 1. Similarly, on 1/3 we had net new visitor E, so the number should be 1
Output:
Date #
2020-01-01 3
2020-01-02 1 (D)
2020-01-03 1 (E)
2020-01-04 1 (F)
My solution:
select a.date, count(distinct a.visitor)
from log a
left join log b on a.visitor = b.visitor and b.date < a.date
where b.visitor is null
group by a.date
Thanks!