Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Learning mySQL but cant figure out this error

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading