…
…
Fix the email, name and phone fields of a club's messy member list with text functions, and find the repeated records of the same person.
The photography club on campus has collected its members through a sign-up form for two years. The form checked no rules, so the list is a mess: some people typed their address in capital letters, some addresses have an invisible space at the end, phone numbers were entered in five different formats, and some members filled in the form twice. The club board will send the first announcement of the term by email and trip reminders by text message. In this project, you will get the list ready for sending: you will find the dirt, count it, produce the clean form and weed out the repeated records.
Data: one table, fictional sample data.
member: id, first_name, last_name, email (can be empty), phone (text; can be empty) (60 records)Lessons used: SELECT, WHERE and IN (3–5), COUNT, CASE, GROUP BY and HAVING (8, 11–13), type conversions and text functions (25–26). Date functions (27) are not needed. There is no new concept; you will apply the method of lesson 26 (first count, then clean, then count again) from start to finish on another list.
The cells contain only comment lines and ___: you will write the query yourself. The first line of every cell starts with "Why this query:"; complete it with one sentence before you write the query (it is not checked; it just makes you think).
Every step only reads data: the functions do not change the table, and the clean form is shown only in the result. You can rerun the cells as often as you like.
What you'll do
members.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The club board will send the first announcement of the term to all members by email. Before sending, you are asked to look at the list: some addresses are in capital letters, and some were entered with a space at the start or the end. An address that looks clean on screen can still be dirty, because a space is invisible.
List the dirty addresses. Each row should have the member's id, the raw form of the address (raw) and its cleaned form (clean). Show the raw address between double quotes, so a space at the edge shows up before or after the quote. The clean address must be lowercase with no spaces at the edges. The list comes in id order.
How many addresses are in the output, and what kinds of dirt are there? In the next step you will check this number with a one-row summary.
This step's code cell is for members.Sign up free
The club president wants to see the state of the list in one row: how many records are there in total, how many have an email, how many are empty and how many are dirty?
Write a one-row summary with four columns: records, with_email, no_email
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
dirtyDo not count empty addresses as dirty: a member without an address is not a cleanup problem, it is a missing-information problem. Decide by looking at the output whether your query puts NULL addresses in the right place by itself.
This step's code cell is for members.Sign up free
The announcement email will start with "Hello First Last". But some members typed their name completely in capital letters, some completely in lowercase, and a few names have a space at the edge.
List the members whose first name or last name is not in the proper form. Proper form: no space at the edges, the first letter of every word capital, the rest lowercase. Columns: id, the raw first_name, the raw last_name and the clean full_name, which joins the two with a space between them. The list comes in id order.
Note: INITCAP capitalizes the first letter of every word and lowercases the rest, and it keeps accented letters like "é" and "ü" working. Even so, look at the corrected names in a real list.
This step's code cell is for members.Sign up free
The club also wants to send members text messages on trip days. Looking at the phone column, you see that the same number is written in different formats: some with spaces, some with dashes, some run together, some with a country code.
First get to know the formats you have. Remove the spaces and dashes from the phone, and count the records by the length of the remaining text. Columns: length, the number of records of that length (records) and one sample phone from that group (sample; the alphabetically first phone of the group is enough). Sort by length, from small to large.
Interpret the output: a US-style phone number is 10 digits; it may also have a leading 1 (11 digits) or a leading +1 (12 characters). Which lengths are another way of writing the same number, and which are really broken? In which row were the records with an empty phone collected?
This step's code cell is for members.Sign up free
In the previous step you saw three valid ways of writing: 10 digits (555...), 11 digits (1555...) and 12 characters (+1555...). The last 10 digits of all three are the same: the number itself. The text message service wants the number in one format: run together, 10 digits (like 5550142123).
Bring the phones of valid length to this format. Columns: id, the raw phone and the clean phone_clean. Phones of broken length and empty phones must not be in the list; the club will fix them by asking the members. The list comes in id order.
Think before you write: how can you get the same result from three different formats with one expression? Which end of the text, and how many characters from it, does the job?
This step's code cell is for members.Sign up free
Some members filled in the form more than once; the same person should not get the announcement twice. A teammate wrote the query below, which finds people with more than one record under the same email. They also added LOWER, thinking about capital letters.
Run the query. Then go back to the dirty address list of step 1: is there an address there that becomes the same as another address once cleaned? Find the repetition the query missed and fix the query. The columns stay the same: email (clean address), records (how many times they signed up) and first_id (the id of the first record); sorted by clean address.
This step's code cell is for members.Sign up free
The cleanup is done; the club president wants to see the final state in one row. Four numbers:
records)unique_email)no_email)phone_valid)The numbers must be consistent with the previous steps: unique_email equals the number of records that have an address, minus the extra repeated records.
Once the output matches, think (it is not checked): how many people will the announcement go to? Can you estimate the number of people in the club as unique_email + no_email, or can there also be repeats among the records without an email? How would you check that by looking at first and last names?
This step's code cell is for members.Sign up free