This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* B2B. Getting daily or monthly retention for free users | |
Input tables: | |
signups - user_id, s.signup_date | |
activity - user_id, activity_date | |
*/ | |
-- CREATE VIEW cohort_user_retention AS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH aggregated_subs AS ( | |
SELECT | |
USER_ID | |
,PRODUCT_ID | |
,TRANSACTION_START | |
,TRANSACTION_DATE_DUE | |
,ROW_NUMBER() OVER (PARTITION BY PRODUCT_ID ORDER BY TRANSACTION_START) AS rn | |
,LAG(TRANSACTION_DATE_DUE, 1) OVER (PARTITION BY PRODUCT_ID ORDER BY TRANSACTION_DATE_DUE) AS previous_sub_end | |
FROM SUBSCRIPTIONS | |
-- ORDER BY TRANSACTION_START, rn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT active.num_days_active | |
, COUNT(DISTINCT active.user_id) AS num_users -- get how many users have a number of active days | |
FROM ( | |
SELECT a.user_id, COUNT(DISTINCT a.created_at) AS num_days_active | |
FROM activity a | |
GROUP BY a.user_id) AS active | |
LEFT JOIN activity a2 ON a2.user_id = active.user_id | |
WHERE a2.created_at >= CURRENT_DATE - 28 | |
GROUP BY active.num_days_active | |
ORDER BY active.num_days_active ASC; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''We have two monkeys, a and b, and the parameters a_smile and b_smile | |
indicate if each is smiling. We are in trouble if they are both smiling | |
or if neither of them is smiling. Return True if we are in trouble.''' | |
''' | |
monkey_trouble(True, True) → True | |
monkey_trouble(False, False) → True | |
monkey_trouble(True, False) → False | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Given an int n, return the absolute difference between n and 21, | |
except return double the absolute difference if n is over 21.''' | |
def diff21(n): | |
if n <= 21: | |
return 21-n | |
else: | |
return (n - 21) * 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''You have the following list of numbers: | |
n = [1,2,3,4,5,6] | |
Iterate over this list, printing out each list value multiplied by 10.''' | |
#Solution 1: | |
for x in n: | |
print (x * 10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzBuzz(n): | |
for i in range(1,n+1): | |
if i % 3 == 0 and i % 5 == 0: | |
print("FizzBuzz") | |
elif i % 5 == 0: | |
print("Buzz") | |
elif i % 3 == 0: | |
print("Fizz") | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- quantitative profiling | |
with profile_1 as ( | |
select user_id | |
from ( | |
select p.user_id as user_id, count(distinct p.item_id) as purchased | |
from user u | |
join tags t on t.id = u.tag_id | |
join purchase p on p.item_id = u.item_id | |
where t.name = 'scarf' | |
group by 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Assign 200 to x. | |
If x > 200, print out "Big"; | |
If x > 100 and x <= 200, print out "Average"; | |
and If x <= 100, print out "Small""" | |
x = 200 | |
if x > 200: | |
print ("Big") | |
elif x > 100 and x <= 200: | |
print ("Average") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. | |
#We are in trouble if the parrot is talking and the hour is before 7 or after 20. | |
#Return True if we are in trouble. | |
def parrot_trouble(talking, hour): | |
#hour < 7 or hour > 20 | |
return (talking and (hour - 7 or hour > 20)) |
NewerOlder