Back to questions
You're provided with two tables: the table contains information about advertisers and their respective payment status, and the table contains the current payment information for advertisers, and it only includes advertisers who have made payments.
Write a query to update the payment status of Facebook advertisers based on the information in the table. The output should include the user ID and their current payment status, sorted by the user id.
The payment status of advertisers can be classified into the following categories:
Before proceeding with the question, it is important to understand the possible transitions in the advertiser's status based on the payment status. The following table provides a summary of these transitions:
# | Current Status | Updated Status | Payment on Day T |
---|---|---|---|
1 | NEW | EXISTING | Paid |
2 | NEW | CHURN | Not paid |
3 | EXISTING | EXISTING | Paid |
4 | EXISTING | CHURN | Not paid |
5 | CHURN | RESURRECT | Paid |
6 | CHURN | CHURN | Not paid |
7 | RESURRECT | EXISTING | Paid |
8 | RESURRECT | CHURN | Not paid |
The transitions between payment statuses in the provided table can be summarized as follows:
Column Name | Type |
---|---|
user_id | string |
status | string |
user_id | status |
---|---|
bing | NEW |
yahoo | NEW |
alibaba | EXISTING |
Column Name | Type |
---|---|
user_id | string |
paid | decimal |
user_id | paid |
---|---|
yahoo | 45.00 |
alibaba | 100.00 |
target | 13.00 |
user_id | new_status |
---|---|
bing | CHURN |
yahoo | EXISTING |
alibaba | EXISTING |
The dataset you are querying against may have different input & output - this is just an example!
The solution is inspired by Sunil Ingaleshwar's approach to addressing the problem.
In this step, we merge the table with the table using a FULL OUTER JOIN. Read this for more insight into SQL joins.
This allows us to combine the information from both tables and ensure that all records are included, even if they don't have a matching in the other table.
The joined table will include all rows from both tables, matched where the values are equal.
Example output (showing random 5 rows out of 9 rows):
user_id | status | user_id | paid |
---|---|---|---|
bing | NEW | NULL | NULL |
yahoo | NEW | yahoo | 45.00 |
alibaba | EXISTING | alibaba | 100.00 |
baidu | EXISTING | NULL | NULL |
NULL | NULL | fitdata | 25.00 |
If you were to query , you would observe that Fitdata is present in the table. However, since Fitdata is not included in the existing advertisers' data stored in the table, the corresponding entries for Fitdata in the merged table obtained through the FULL OUTER JOIN would appear as NULL.
In this step, we assign the payment status to each user based on their payment information. We use a statement to handle different conditions and assign the appropriate status.
The payment status of advertisers can be classified into the following categories:
In this step, we select the user IDs along with their adjusted payment status by using the COALESCE function to combine the columns from both tables.
The final step is to order them in ascending order.