MySQL throwing error 1064, doesn't seem to recognize OVER command - mysql

My query is:
SELECT *,
ROW_NUMBER() OVER (ORDER BY score ASC)
FROM submissions
The error message I receive is:
#1064 - 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 '(ORDER BY score ASC) FROM submissions LIMIT 0, 25' at line 2
I am running this query in phpMyAdmin. I notice that OVER is not colored blue, nor does is it suggested as I type, unlike other command words (ORDER, ASC, etc).
This simpler query runs just fine:
SELECT * FROM submissions
I've tried putting things in quotes, using the RANK function instead, and fiddling with whitespace, but the query still doesn't run. What is wrong here?

My guess is that you are running a version of MySQL which is earlier than 8+, one which does not support ROW_NUMBER. There are a few options for simulating ROW_NUMBER in earlier versions of MySQL. One is to use user variables:
SELECT *,
(#row_number:=#row_number + 1) AS rn
FROM submissions, (SELECT #row_number := 0) tmp
ORDER BY score;

Related

Understanding the rational behind ORDER BY in sql

The following query works
SELECT score, DENSE_RANK() OVER(ORDER BY score DESC) AS 'rank' FROM Scores
But when I do the following below it doesn't work
SELECT score, DENSE_RANK() OVER(ORDER BY score DESC) AS 'rank' FROM Scores ORDER BY rank
RANK is a reserved keyword in MySQL 8.0, see https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-R
In your ORDER BY clause, the parser thinks you are using the RANK function, and it gets confused when it reaches the end and finds you have not put () after the function name RANK. The error shows what follows the point where it got confused, which is the end of the query, so what follows is ''.
ERROR 1064 (42000): 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 '' at line 1
You can fix this and use reserved keywords as identifiers by enclosing them in back-ticks:
SELECT score, DENSE_RANK() OVER(ORDER BY score DESC) AS 'rank'
FROM Scores
ORDER BY `rank`;

User defined variable as alias MySQL v8.0 not working

The following query (this is a condensed form) is running fine with user defined variables selected as an alias on MySQL v5.6.20 BUT breaks on MySQL v8.0.23
SELECT *, #rank := #rank + 1 AS rank
FROM q39wg_comments;
Error:
ERROR 1064 (42000): 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 'rank
So I finally figured it out, It was happening because rank is now a reserved keyword in MySQL 8.x.x as described here.
Once I updated the variable name, the query started working.
Another alternative is to use back-ticks which I think is more future proof as more keywords get reserved, it will prevent your SQL from breaking unnecessarily. Eg. The following works fine on MySQL v8.x.x
Eg:
SELECT *, #rank := #rank + 1 AS `rank`
FROM q29wg_jreviews_comments ;
you must initialize #rank
try this:
SELECT *, #rank := #rank + 1 AS myrank
FROM q39wg_comments
JOIN ( SELECT #rank:=0 ) AS init;
see https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=97b2c82bac1c4c2f5008a6e5f8298e04

SubQuery and ROWNUM in MySQL : SELECT * FROM (SELECT...)

EDIT: This solved my problem:
select id_t,punctaj
from test
where punctaj in (select punctaj from test )
order by punctaj desc
limit 1;*
I have written a working Oracle SQL code but when I'm trying to convert it to MySQL I have a syntax error which I cannot solve. It looks like MySQL does not accept ROWNUM and also SELECT * FROM a subquery. What could be the solution? I need the biggest value of "PUNCTAJ" from the tests and I need to keep that "IN". Thanks!
Here is my code:
select* from (
select id_t,punctaj
from test
where punctaj in (select punctaj from test )
order by punctaj desc)
where rownum<=1
error:#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where rownum<=1 LIMIT 0, 25' at line 6
you have written twice the where condition,
In secondd where use AND rownum<=1

Totals and Runningtotals with SQL "sum" and "over" Syntax

In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
MySQL said: Documentation
#1064 - 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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country

sql mariadb, error syntax over partition

When executing a query I get following error
'[Err] 1064 - You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near '(PARTITION by kd_lv3) as max_date , row_number()
OVER (PARTITION by kd_' at line 6'.
Query :
select kd_lv3
, nm_lv3
, kd_lv2
, kd_lv1
, date(update_date) as update_date
, max(date(update_date)) OVER (PARTITION by kd_lv3) as max_date
, row_number() OVER (PARTITION by kd_lv3) as rownum
from akun_lv3_dump
What should I do to resolve the error?
I would have commented instead of answer, but I don't have points enough.
The SQL looks good to me, but I dug into it and your query has:
(PARTITION by kd_lv3) as max_date
while your error has:
(PARTITION by kd_akun_lv3)
So, if the query you supplied is correct, that isn't what is being run - and you are possibly testing some older sql. Not sure what your environment is, but perhaps something needs refreshing...