Escolar Documentos
Profissional Documentos
Cultura Documentos
Ejemplo:
CREATE TABLE celebs (id INTEGER, name TEXT, age INTEGER);
This statement filters the result set to only include movies with names that begin with
letters "A" up to but not including "J".This statement filters the result set to only include
movies with names that begin with letters "A" up to but not including "J".
n this statement, the BETWEEN operator is being used to filter the result set to only include
movies with years between 1990 up to and including 2000.
We can make a few changes to our Daily Count query to get the revenue. First,
instead of using count(1) to count the rows per date, we'll
useround(sum(amount_paid), 2) to add up the revenue per date. Complete the
query by adding revenue per date. Second, we need to join in the order_items table
because that table has an amount_paid column representing revenue. Complete
the query by adding ajoin clause whereorders.id = order_items.order_id.
select date(ordered_at), round(sum(amount_paid),2) from orders join
order_items on orders.id=order_items.order_id
group by 1
order by 1;
Nice. Now with a small change, we can find out how much we're making per day
for any single dish. What's the daily revenue from customers ordering kale
smoothies?
Complete the query by using a where clause to filter the daily sums down to orders
where the name = 'kale-smoothie'.
select date(ordered_at), round(sum(amount_paid),2) from orders join
order_items on orders.id=order_items.order_id
where name='kale-smoothie'
group by 1
order by 1;
We'll look at the data several different ways, the first of which is determining what percent
of revenue these smoothies represent.
To get the percent of revenue that each item represents, we need to know the total
revenue of each item. We will later divide the per-item total with the overall total revenue.
select name, round(sum(amount_paid),2) from order_items
group by 1
order by 2 desc;
Complete the denominator in the subquery, which is the total revenue fromorder_items.
Use the sum function to query the amount_paid from theorder_items table.
select name,
round( sum(amount_paid)/(select sum(amount_paid)
from order_items)*100.0, 2) as pct
from order_items
group by 1
order by 2 desc;
To see if our smoothie suspicion has merit, let's look at purchases by category. We
can group the order items by what type of food they are, and go from there. Since
our order_items table does not include categories already, we'll need to make
some!
Previously we've been using group by with a column (like order_items.name) or a
function (likedate(orders.ordered_at)).
We can also use group by with expressions. In this case a case statement is just
what we need to build our own categories. case statements are similar toif/else in
other languages.
Here's the basic structure of a case statement:
case {condition}
when {value1} then {result1}
when {value2} then {result2}
else {result3}
end
We'll build our own categories using a casestatement. Complete the query below with
acase condition of name that lists out each product, and decides its group
Complete the query by using the category column created by the case statement in our
previous revenue percent calculation. Add the denominator that will sumthe amount_paid
While we do know that kale smoothies (and drinks overall) are not driving a lot of
revenue, we don't know why. A big part of data analysis is implementing your own
metrics to get information out of the piles of data in your database.
In our case, the reason could be that no one likes kale, but it could be something
else entirely. To find out, we'll create a metric called reorder rate and see how
that compares to the other products at SpeedySpoon.
We'll define reorder rate as the ratio of the total number of orders to the number
of people making those orders. A lower ratio means most of the orders are
reorders. A higher ratio means more of the orders are first purchases.
Let's calculate the reorder ratio for all of SpeedySpoon's products and take a look
at the results. Counting the total orders per product is straightforward. We count
the distinct order_ids in the order_itemstable.
Complete the query by passing in thedistinct keyword and the order_idcolumn
name into the count function
To get that information, we need to join in the orders table and count unique values
in the delivered_to field, and sort by the reorder_rate.
Complete the query below. The numerator should count the distinct order_ids. The
denominator should count the distinct values of the orders table'sdelivered_to field
(orders.delivered_to).
Mineblocks is a game, and one of the core metrics to any game is the number
people who play each day. That KPI is called Daily Active Users, or DAU.
DAU is defined as the number of unique players seen in-game each day. It's
important not to double count users who played multiple times, so we'll
usedistinct in our count function.
Likewise, Weekly Active Users (WAU) and Monthly Active Users (MAU) are in the
same family.
Previously we calculated DAU only per day, so the output we wanted was [date,
dau_count]. Now we want DAU per platform, making the desired output [date,
platform, dau_count].
Calculate DAU for Mineblocks per-platform. Complete the query below. You will
need to select the platform column and add a countfunction by passing in
the distinct keyword and the user_id column name.
players: users who play the game but have not yet purchased
The next KPI we'll look at Daily ARPPU - Average Revenue Per Purchasing User.
This metric shows if the average amount of money spent by purchasers is going up
over time.
Daily ARPPU is defined as the sum of revenue divided by the number of
purchasers per day.
The more popular (and difficult) cousin to Daily ARPPU is Daily ARPU, Average
Revenue Per User. ARPU measures the average amount of money we're getting
across all players, whether or not they've purchased.
ARPPU increases if purchasers are spending more money. ARPU increases if
more players are choosing to purchase, even if the purchase size stays consistent.
No one metric can tell the whole story. That's why it's so helpful to have many KPIs
on the same dashboard.
Building on this CTE, we can add in DAU from earlier. Complete the query by
calling the DAU query we created earlier, now aliased as daily_players:
Now that we have the revenue and DAU, join them on their dates and calculate
daily ARPU. Complete the query by adding the keyword using in the join clause.
Nice work, you just defined ARPU for Mineblocks! In our ARPU query, we used
using instead of on in the join clause. This is a special case join.
When the columns to join have the same name in both tables you can use using instead
of on. Our use of the using keyword is in this case equivalent to this clause:
Now let's find out what percent of Mineblock players are returning to play the
next day. This KPI is called 1 Day Retention.
Retention can be defined many different ways, but we'll stick to the most basic
definition. For all players on Day N, we'll consider them retained if they came
back to play again on Day N+1.
This will let us track whether or not Mineblocks is getting "stickier" over time.
The stickier our game, the more days players will spend in-game.
And more time in-game means more opportunities to monetize and grow our
business.
Before we can calculate retention we need to get our data formatted in a way
where we can determine if a user returned.
Currently the gameplays table is a list of when the user played, and it's not
easy to see if any user came back.
By using a self-join, we can make multiple gameplays available on the same
row of results. This will enable us to calculate retention.
The power of self-join comes from joining every row to every other row. This
makes it possible to compare values from two different rows in the new result
set. In our case, we'll compare rows that are one date apart from each user.
To calculate retention, start from a query that selects the date(created_at) as dt
and user_id columns from the gameplays table.
Now we'll join gameplays on itself so that we can have access to all gameplays for
each player, for each of their gameplays.
This is known as a self-join and will let us connect the players on Day N to
the players on Day N+1. In order to join a table to itself, it must be aliased so we
can access each copy separately.
We aliased gameplays in the query above because in the next step, we need to
joingameplays to itself so we can get a result selecting [date, user_id,
user_id_if_retained].
The previous query joined all rows in gameplays against all other rows for each
user, making a massive result set that we don't need.
We'll need to modify this query.
Now that we have our gameplays table joined to itself, we can start to calculate
retention.
1 Day Retention is defined as the number of players who returned the next day
divided by the number of original players, per day. Suppose 10 players played
Mineblocks on Dec 10th. If 4 of them play on Dec 11th, the 1 day retention for Dec
10th is 40%.
The query above won't return meaningful results because we're using an inner join.
This type of join requires that the condition be met for all rows, effectively limiting
our selection to only the users that have returned.
Instead, we want to use a left join, this way all rows in g1 are preserved, leaving
nulls in the rows from g2 where users did not return to play the next day.
Change the join clause to use left joinand count the distinct number of users
fromg1 and g2 per date.
Now that we have retained users as count(distinct g2.user_id) and total users as
count(distinct g1.user_id), divide retained users by total users to calculate 1 day
retention!