SELECT created_at, grand_total,
lag(grand_total,1) over (order by o.grand_total desc) as 'lag'
FROM sales_flat_order;
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 o.grand_total desc) as 'lag' FROM
sales_flat_order' at line 2 0.016 sec
code fails at: over **(**order by o.grand_total desc)
Check which MySQL version are you using
Simulate lag function in MySQL
MySql using correct syntax for the over clause
You can use a correlated subquery:
SELECT created_at, grand_total,
(select grand_total
from sales_flat_order sfo2
where sfo2.grand_total > sfo.grand_total
order by sfo2.grand_total asc
limit 1
)
FROM sales_flat_order sfo;
Note that lag() with a descending sort is more commonly called "lead".
Related
Hello, I have the following problem, I use the world.sql schema and I can't display my query in a descending way, I get the following 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 'DESC'
I can't find a way to solve the problem other than storing the result in a table and then applying DESC. Could someone guide me what I could do or where I could investigate?
SELECT la.language, la.Isofficial AS Isofficial ,SUM(country.Population * la.Percentage / 100) pop_tot FROM country INNER JOIN countrylanguage AS la ON la.countryCode = country.Code WHERE la.language != "Spanish" AND la.Isofficial = "T" GROUP BY la.Language DESC ;
'Prior to MySQL 8.0.13, MySQL supported a nonstandard syntax extension that permitted explicit ASC or DESC designators for GROUP BY columns...As of MySQL 8.0.13, the GROUP BY extension is no longer supported: ASC or DESC designators for GROUP BY columns are not permitted.' - https://dev.mysql.com/doc/refman/8.0/en/select.html
Replace
GROUP BY la.Language DESC;
with
GROUP BY la.Language
ORDER BY la.Language DESC;
Which will work in whatever MySQL server version you have.
BTW, I've tested the first syntax and it works well in version 5.5
I have query in my structure trying run in PHPMYADMIN. I am trying to run CUBE query for OLAP operation, this is my query :
SELECT QUARTER, REGION, SUM(SALES)
FROM salestable
GROUP BY CUBE (QUARTER, REGION)
I have also tried this query :
SELECT salestable.QUARTER, salestable.REGION, SUM(salestable.SALES)
FROM salestable
GROUP BY CUBE (salestable.QUARTER, salestable.REGION)
but it showing this 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 '(QUARTER, REGION) LIMIT 0, 25' at line 3
I tried to open the page to check syntax but it shows page not found.
I don't think MySQL supports CUBE as a GROUP BY modifier, but you can use WITH ROLLUP:
SELECT st.QUARTER, st.REGION, SUM(st.SALES)
FROM salestable st
GROUP BY st.QUARTER, st.REGION WITH ROLLUP;
I have recently upgraded my MariaDB 5.5 to MariaDB 10.3.14 specifically to be able to use the RANK() OVER function that was introduced after 10.2.
After going through the upgrade process, I still get the same syntax error message I was getting on 5.5.
#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 'RANK() OVER (
ORDER BY
at line 2
I'm trying to understand why after the upgrade I still get the same error.
Here's the query I'm using:
SELECT member_id, total_pts, correct_predictions, correct_goals
RANK() OVER (
ORDER BY
total_pts DESC,
correct_predictions DESC,
correct_goals DESC
) rank
FROM quiniela_1;
What am I doing wrong?
Put a comma after SELECT member_id, total_pts, correct_predictions, correct_goals so that your query reads:
SELECT member_id, total_pts, correct_predictions, correct_goals,
RANK() OVER (
ORDER BY
total_pts DESC,
correct_predictions DESC,
correct_goals DESC
) rank
FROM quiniela_1;
I am trying to create a mysql script that will search for results while at the same time filtering out duplicates of a particular field, I did some research and the code I found came in the format of something like this:
SELECT * FROM sites s INNER JOIN (SELECT h.*, row_number() over (PARTITION by muleid) as seqnum from history h ) h WHERE acceptid=2;
But it came up with the error:
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 '(PARTITION by muleid) as seqnum from history h ) h WHERE acceptid=2' at line 1
Is there anyone that can help?
i was trying to display recent record.
i insert sql below in the recordset
SELECT `date`, `item_name`, `stock_invent`, `id_invent`,`num_in_out`
FROM `inventories` ORDER BY `id_invent` DESC LIMIT 3
But, how to make it display in table? because i got message
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 'LIMIT 0, 10' at line 1
SELECT date, item_name, stock_invent, id_invent, num_in_out
FROM inventories ORDER BY id_invent DESC LIMIT 0,3