代码之家  ›  专栏  ›  技术社区  ›  Suyash

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

  •  0
  • Suyash  · 技术社区  · 1 年前
    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语法有错误;在第26行的“as main)”附近使用正确的语法,请查看与MySQL服务器版本对应的手册

    我试图添加top查询,因为我必须对它执行更多操作。

    1 回复  |  直到 1 年前
        1
  •  0
  •   nbk    1 年前

    必须删除多个括号

    将所有括号与左边保持相同距离有助于获得干净的代码

    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