SQL Programming


#1

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

I don’t know why my SQL codes don’t work:

https://www.codecademy.com/programs/2ab5e673cf4598ee4720e1a98db36328/content-items/df49957b082d326e9985f62fea129590

here’s the code below (calculating the churn rate):

with months as
(select
‘2017-01-01’ as first_day,
‘2017-01-31’ as last_day
union
select
‘2017-02-01’ as first_day,
‘2017-02-28’ as last_day
union
select
‘2017-03-01’ as first_day,
‘2017-03-31’ as last_day
),
cross_join as
(select * from subscriptions
cross join months
),
status as
(select
id,
first_day as month,
case
when (subscription_start < first_day)
and (subscription_end > first_day or subscription_end is null) and (segment = 87) then 1
else 0
end as is_active_87,
case
when (subscription_start < first_day) and (subscription_end > first_day or subscription_end is null) and (segment = 30) then 1
else 0
end as is_active_30,
case
when (subscription_end between first_day and last_day)
then 1
else 0
end as is_canceled_87,
case
when (subscription_end between first_day and last_day)
then 1
else 0
end as is_canceled_30,
from cross_join
), status_aggregate as
(select month,
sum(is_active_87) as sum_active_87,
sum(is_active_30) as sum_active_30,
sum(is_canceled_87) as sum_canceled_87,
sum(is_canceled_30) as sum_canceled_30
from status
group by month
) select month,
1.0sum_canceled_87/sum_active_87 as churn_rate_87,
1.0
sum_canceled_30/sum_active_30 as churn_rate_30
from status_aggregate;


#2

This link doesn’t work for me, can you please give me the course name and step number.


#3

I copied and pasted this and it still doesn’t work. It is giving me an error of “Error: near line 1: near “;”: syntax error” I’ve tried everything, what did I do wrong?