Ctr

Subscribers

Suppose you're working on a content streaming app like Netflix which requires a subscription. Users can either have a monthly or an annual billing cycle. We have a table called transactions to store all payments for subscriptions.


transactions
column_name type
id
INTEGER
subscriber_id
INTEGER
period
VARCHAR
amount_usd
INTEGER
transaction_date
DATE

Hopefully by now, you've gotten used to seeing an id column for each table. We have it here to. This is a unique identifier for each row (each transaction) in the table, which has no duplicates. Every table should have an ID, though it doesn't need to be called id.

There is also a subscriber_id. This represents the person who is making the transaction. Since a subscription involves repeated payments, each subscriber has many transactions. Hence, the same subscriber_id may show up many times, whereas the transaction ID, id, ie unique in this table.

Notice that we also have a new type, DATE. Intuitively this represents a date, for example July 4th 1776, though in databases it's usually written like 1776-07-04.

For this challenge, we want to select a list of subscriber_ids that had a valid subscription on 2022-01-01. Some of these transactions are for monthly billing cycles, and some are annual. A user has a valid subscription on 2022-01-01 if they have a monthly transaction within 1 month before 2022-01-01 or an annual transaction within 1 year before 2022-01-01.

Hints

  • Monthly or annual look like 2 "cases" you need to handle.
  • You can create an instance of a DATE by casting it from a string literal, CAST('2022-01-01' AS DATE), review CAST if you need.
  • DATEs can be compared with operators just like numbers can, = < > <= >= !=.
  • You can add or subtract intervals of time from DATEs using the INTERVAL type, e.g. CAST('2022-01-01' AS DATE) - CAST('1 month' AS INTERVAL).

© 2022 Andrew Carlson. All rights reserved.

id
INTEGER
subscriber_id
INTEGER
period
VARCHAR
amount_usd
INTEGER
transaction_date
DATE
1
18678
6045
annual
125
2021-03-19
2
66845
2428
monthly
12
2021-12-28
3
97315
3503
monthly
12
2021-12-13
4
61414
5888
annual
125
2021-07-05
5
51368
5134
monthly
12
2021-12-10
6
54295
3889
annual
125
2021-09-02
7
87600
3118
monthly
12
2021-12-20
8
31310
5666
annual
125
2021-08-01
9
72633
6329
monthly
12
2021-12-10
10
47284
4015
annual
125
2021-06-10
11
2907
9785
monthly
12
2021-12-17
12
8857
3434
monthly
12
2021-12-18
13
47798
7596
monthly
12
2021-12-08
14
72840
3767
annual
125
2021-06-06
15
85296
8222
monthly
12
2021-12-29
15 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?