How to fix SQL error "Incorrect syntax near the keyword 'OVER'"

favour eigbe 20 Reputation points
2024-03-07T12:22:06.7366667+00:00

I am getting the following error when running my SQL query:

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'OVER'.

Here is my query:

Select cd.continent, cd.location, cd.date, cd.population, cv.new_vaccinations 
, SUM((CONVERT(bigint,cv.new_vaccinations)) OVER (partition by cd.location order by cd.location, cd.Date) as RollingPeopleVaccinated
From CovidData..CovidDeaths cd
Join CovidData..CovidVaccination CV
    On cd.location = cv.location
    and cd.date = cv.date
Where cd.continent is not null
Order by 2,3  

What can I do to fix this error?

SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Naomi Nosonovsky 8,431 Reputation points
    2024-03-07T14:10:42.1333333+00:00

    There is an extra ( before sum, it should be:

    Select cd.continent, cd.location, cd.date, cd.population, cv.new_vaccinations  , SUM(CONVERT(bigint,cv.new_vaccinations)) OVER (partition by cd.location order by cd.location, cd.Date) as RollingPeopleVaccinated From CovidData..CovidDeaths cd Join CovidData..CovidVaccination CV     On cd.location = cv.location     and cd.date = cv.date Where cd.continent is not null Order by 2,3  
    
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2024-03-07T12:29:11.72+00:00

    Which SQL Server version are you using?

    SELECT @@VERSION
    

    AFAIK the windows aggregation SUM...OVER was first introduced in SQL Server 2016.


  2. favour eigbe 20 Reputation points
    2024-03-07T14:19:19.58+00:00

    Select cd.continent, cd.location, cd.date, cd.population, cv.new_vaccinations

    , SUM(CONVERT(bigint,cv.new_vaccinations)) OVER (partition by cd.location order by cd.location, cd.Date) as RollingPeopleVaccinated

    From CovidData..CovidDeaths cd

    Join CovidData..CovidVaccination CV

    On cd.location = cv.location and cd.date = cv.date Where cd.continent is not null

    Order by 2,3

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.