SQL ROW_NUMBER gives error - mysql

I need to order rows in MySQL and assign a number to each row according to that order. ORDER BY is working as intended but not ROW_NUMBER().
This works:
USE my_database;
SELECT
id
,volume
FROM my_table
ORDER BY volume;
This does not work:
USE my_database;
SELECT
id
,volume
,ROW_NUMBER() over(ORDER BY volume)
FROM my_table
ORDER BY volume;
I get this error message:
SELECT id ,volume ,ROW_NUMBER() over(ORDER BY volume) FROM my_table ORDER BY volume Error Code: 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 volume) FROM my_table ORDER BY vol' at line 4 0.000 sec
What am I doing wrong and how do I make it work?
I also tried RANK() and DENSE_RANK() which gives the same problem.

There are no such things as ROW_NUMBER() or RANK() in MySQL. Try the following :
USE my_database;
SET #row_number = 0;
SELECT id
, volume
, #row_number := #row_number + 1 AS rank
FROM my_table
ORDER BY volume;

The function ROW_NUMBER() does not exist in MySQL.
However, you can replicate it, possibly: http://www.mysqltutorial.org/mysql-row_number/
The row_number is a ranking function that returns a sequential number
of a row, starting from 1 for the first row. We often want to use the
row_number function to produce the specific reports we need.
Unfortunately, MySQL does not provide row_number like Microsoft SQL
Server and Oracle. However, in MySQL, you can use session variables to
emulate the row_number function.
example:
SET #row_number = 0;
SELECT
(#row_number:=#row_number + 1) AS num, firstName, lastName
FROM
employees
LIMIT 5;

MySQL introduced the ROW_NUMBER() function since version 8.0.

Related

MySQL Warning: #1287 Setting user variables within expressions is deprecated and will be removed in a future release

I use this query to rewrite the id number column by date, after a new row is added to the database.
Even if the query runs well I can't fix the error displayed at the end of the query. Any suggestion?
SET #ROW = 0;
UPDATE `mytable` SET `id` = #ROW := #ROW+1 ORDER BY `date` ASC;
Warning: #1287 Setting user variables within expressions is deprecated and will be removed in a future release. Consider alternatives: 'SET variable=expression, ...', or 'SELECT expression(s) INTO variables(s)'.
I tried to modify the query
set id = "0";
UPDATE `mytable` SET := id+1 ORDER BY `data` ASC;
with no success.
User variables are mostly superseded with window functions, available in MySQL 8.0.
You can what you ask for with row_number() and the update/join syntax :
update mytable t
inner join (select id, row_number() over(order by date, id) new_id from mytable) t1
on t.id = t1.id
set t.id = t1.new_id
Demo on DB Fiddlde.
This assumes that id is a unique key to start with.
I would still question why you would need to alter what looks like a surrogate primary key. You can compute the row number on the fly in your queries in that's what you want, or use a view :
create view myview as
select t.*, row_number() over(order by date, id) new_id from mytable t
Demo on DB Fiddlde

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`;

Alternative to using Variable in a View

I need some assistance building a SQL statement that will output the top 5 retired assets per client that can be put into a SQL View.
I have built the following SQL statement but it will not work within a view and need an alternative.
SET #row_number := 0;
SELECT DISTINCT NAME, RetiredDate, COMMENT,
#row_number:=CASE WHEN #client_ID=clientID THEN #row_number+1 ELSE 1 END AS num,
#client_ID:=clientID ClientID
FROM `retiredassets`
WHERE `retiredassets`.`ClientID` IN(SELECT clientID FROM `clients`)
HAVING num <=5
Does anyone have any suggestions for me? The above statement works flawlessly but cannot work within a SQL View.
In MySQL 8.0 this should be:
WITH cte AS (
SELECT r.NAME, r.RetiredDate, r.COMMENT,
ROW_NUMBER() OVER (PARTITION BY r.ClientID ORDER BY ...) AS num,
r.ClientID
FROM retiredassets
JOIN clients USING (ClientID)
)
SELECT * FROM cte WHERE num <= 5;
I left ... because I don't know what your ordering is. Your original query doesn't specify an order, so you'll have to choose one.
Re your comment.
If you can't upgrade to MySQL 8.0, and you can't use variables in a VIEW definition, then you can't use a VIEW. You will have to write out the full query when you use it, until you can upgrade to MySQL 8.0.

I have no idea why these SQL command doesnt work

So I have an Xampp server with phpmyadmin and I usered this command, which always worked:
WITH temptable AS (SELECT *, ROW_NUMBER() OVER (ORDER BY character_ID DESC)
AS t FROM characters) SELECT * FROM temptable WHERE t BETWEEN 0 AND 10;
Now I got a database on a real server and it doesent work anymore. So I looked for other commands on the internet and always found this one on several websites:
SELECT
ROW_NUMBER() OVER(ORDER BY Character_ID ASC) AS t,
Charactername
FROM characters WHERE t < 5;
I looked over several websites and it doesnt work for me and i dont know why.
The syntax in Phpmyadmin doesnt mark anything wrong when i write this command but i got still the error:
You have an error in your SQL syntax;
Maybe someone knows why?
You cannot filter by columns defined in the SELECT. If you are using MySQL (or MariaDB), you can use the HAVING clause:
SELECT ROW_NUMBER() OVER(ORDER BY Character_ID ASC) AS t,
Charactername
FROM characters
HAVING t < 5;
These databases extend the use of the HAVING clause for non-aggregation queries, and it allows the use of column aliases for filtering.
Filtering aggregation doesn't work that way in select.Modify your query as below,
SELECT * FROM
(
SELECT
ROW_NUMBER() OVER(ORDER BY Character_ID ASC) AS t,
Charactername
FROM characters) Characternametab
WHERE t < 5;

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

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;