select * from
(
select year,
subs,
unsubs,
LAG(subs,1) OVER(ORDER BY year) as sub_py,
LAG(unsubs,1) OVER(ORDER BY year) as unsub_py
FROM
(
(
SELECT
DATE_FORMAT(subscription_started,"%Y") as year,
SUM(Case WHEN subscription_started is not null then 1 else 0 end) as subs
FROM user_churn
group by year
) as s
INNER JOIN
(
SELECT
DATE_FORMAT(subscription_ended,"%Y") as year_unsub,
SUM(Case WHEN subscription_ended is not null then 1 else 0 end) as unsubs
FROM user_churn
group by year_unsub
) as us
on s.year=us.year_unsub
) as main
)
The sub-query is working fine, but it fails when I add the topmost query. Error is on DBFiddle
Query Error: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘as main )’ at line 26
I am trying to add top query because I have to perform more operations on it.
>Solution :
You must remove more than one bracket
Keeping all brackets on the same distance from he left is helpful to have clean code
select * from
(
select year,
subs,
unsubs,
LAG(subs,1) OVER(ORDER BY year) as sub_py,
LAG(unsubs,1) OVER(ORDER BY year) as unsub_py
FROM
(
SELECT
DATE_FORMAT(subscription_started,"%Y") as year,
SUM(Case WHEN subscription_started is not null then 1 else 0 end) as subs
FROM user_churn
group by year
) as s
INNER JOIN
(
SELECT
DATE_FORMAT(subscription_ended,"%Y") as year_unsub,
SUM(Case WHEN subscription_ended is not null then 1 else 0 end) as unsubs
FROM user_churn
group by year_unsub
) as us
on s.year=us.year_unsub
) as drv