学习mySQL,但无法解决此错误

3npbholx  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(67)
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
)

字符串
子查询工作正常,但当我添加最顶层的查询时,它失败了。
查询错误:错误:ER_PARSE_ERROR:您的SQL语法中有一个错误;请检查与您的MySQL服务器版本对应的手册,以获得在第26行'as main)'附近使用的正确语法
我试图添加顶级查询,因为我必须对它执行更多操作。

i2byvkas

i2byvkas1#

必须删除多个括号
保持所有括号与左边的距离相同有助于代码的整洁

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

字符串

相关问题