-3

The error is in the first ORDER BY which is on the second line. The code runs perfectly fine, if I remove the first ORDER BY.

Here is the code :

SELECT
    dea.continent,
    dea.location,
    dea.date,
    dea.population,
    vac.new_vaccinations,
    SUM(CAST(vac.new_vaccinations AS BIGINT)) OVER (PARTITION BY dea.location ORDER BY dea.location, dea.date)
FROM 
    PortfolioProject..CovidDeaths dea
JOIN 
    PortfolioProject..CovidVaccinations vac ON dea.location = vac.location
                                            AND dea.date = vac.date
WHERE 
    dea.continent IS NOT NULL
ORDER BY 
    2,3

Here is the error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'order'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Posting your SQL code as text instead of as an image as well as adding the table schemas so people can repeat the problem locally is a better way to write a question about SQL. Only getting an image without other context makes it a lot of work to answer the question in a good way. – Joachim Isaksson Aug 22 '22 at 04:24
  • Bro i don't know how to do that. Pls help if you can im stuck on this problem for the past 2 days. That's why i created an account here to ask the question. I can't add table schemas because i imported an Excel file. if you want i can post the code as text but pls solve this. – Divyajeet Singh Aug 22 '22 at 04:40
  • 1
    Yes, at least just copy and paste the SQL text here. You can use the `{}` button when editing your request, to get the SQL formatted. You should also copy and paste the error message. [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Thorsten Kettner Aug 22 '22 at 04:49
  • And please tell us what DBMS you are using. SQL questions should always be tagged with the DBMS, because answers may heavily depend on it. – Thorsten Kettner Aug 22 '22 at 04:57
  • I did all what you guys said, Forgive me for these silly mistakes. It's my first question here. – Divyajeet Singh Aug 22 '22 at 05:02
  • 3
    I've just simply googled `Msg 102 "near 'order'"`. From the search results it is ovious that your old SQL Server version does not support `SUM(...) OVER (... ORDER BY)`. Maybe this helps: `https://stackoverflow.com/questions/55276437/msg-102-level-15-state-1-line-28-incorrect-syntax-near-order. But maybe you should rather just upgrade your outdated SQL Server version. – Thorsten Kettner Aug 22 '22 at 05:07
  • @ThorstenKettner Ohh all this time i was checking the code and the problem wasn't in it. Thank you so much bro i was going crazy for the past 2 days. – Divyajeet Singh Aug 22 '22 at 06:18

1 Answers1

1

Thanks to @Thorsten Kettner. He told the answer in the comments. The issue was not in the code but it was in the outdated version of Sql Server Studio that i am using. Msg 102, Level 15, State 1, Line 28 Incorrect syntax near 'order'

  • SSMS is completely unrelated to the error - it is the version of the database server instance that is your problem. – SMor Aug 22 '22 at 11:11