Skip to content

Instantly share code, notes, and snippets.

View ks--ks's full-sized avatar
🎯
Focusing

Olga Berezovsky ks--ks

🎯
Focusing
View GitHub Profile
@ks--ks
ks--ks / retention_cohorted_example.sql
Last active January 4, 2024 22:34
Multiple ways to get cohorted user retention in SQL
/* 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
@ks--ks
ks--ks / subscription_id.sql
Created August 10, 2022 05:22
A way to generate a subscription id and group transactions per subscription per user
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
@ks--ks
ks--ks / UserEngagementHistogram.sql
Last active May 6, 2023 18:05
User Engagement Histogram
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;
'''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
'''
'''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
'''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)
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:
@ks--ks
ks--ks / UserProfiling.sql
Created July 12, 2020 00:33
Create 4 user profiles based on user actions
-- 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)
""" 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")
@ks--ks
ks--ks / PythonFun_Parrot.py
Created July 3, 2020 22:41
A quick Python exercise
#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))