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
)
子查询工作正常,但当我添加最上面的查询时,它失败了。 DBFiddle
出现错误查询错误:错误:ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 26 行“as main )”附近使用的正确语法
我正在尝试添加顶级查询,因为我必须对其执行更多操作。
您必须移除多个支架
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