'I cannot create CTE even though the query inside the CTE run successfully [closed]

I don't know why but for some reason I cannot create a CTE.

WITH CTE AS
(
    SELECT 
        p1.country_code,
        (p2.gdp_per_capita - p1.gdp_per_capita) / p1.gdp_per_capita AS 'growth_percent'
    FROM 
        per_capita p1, per_capita p2
    WHERE 
        p1.country_code = p2.country_code 
        AND p1.year = p2.year - 1 
)

The query inside the CTE run perfectly fine but when I try to run all of this it returns this message

Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ')'



Solution 1:[1]

You need a valid sql statement after the closing parenthesis of the CTE, most likely making use of the CTE:

WITH CTE AS
(SELECT p1.country_code,
(p2.gdp_per_capita - p1.gdp_per_capita)/p1.gdp_per_capita as 'growth_percent'
FROM per_capita p1, per_capita p2
WHERE p1.country_code = p2.country_code AND p1.year = p2.year-1 )
SELECT * FROM CTE

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zakaria