I HAVE A QUERY LIKE THIS:
CREATE VIEW dashboard
AS
SET #prevValuepo := 0;
SET #prevValuefc := 0;
SELECT *,po-#prevValuepo AS selisihpo ,#prevValuepo:= po,forecast-#prevValuefc AS selisihfc ,#prevValuefc:= forecast
FROM view_t_dasboard WHERE tahun= '2020' ORDER BY bulan ASC
BUT WHY CAN'T CREATE TABLE VIEW
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 'SET #prevValuepo :=0, #prevValuefc :=0,
po-#prevValuepo AS selisihpo ,#prevValu' at line 4
You can't have these SET statemments inside the CREATE VIEW statement. Also, since the order of evaluation of expressions in the select clause is undefined, your query might not even do what you want consistently.
If you are running MySQL 8.0, I would recommend window functions instead of user-defined variables. I think the logic you want is:
create view dashboard as
select
t.*,
po - lag(po, 1, 0) over(order by bulan) as selisihpo,
forecast - lag(forecast, 1, 0) over(order by bulan) as selisihfc
from view_t_dasboard t
where tahun= '2020'
Related
Here is an error I don't understand:
mysql> UPDATE gp
-> SET gp.gpid = gp.new_gpid
-> FROM (
-> SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
-> FROM gp
-> ) gp;
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 'FROM (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY [gpid]) AS new_gpid
' at line 3
As far as I can tell nested SELECT in a FROM statement seems to be depreciated.
Am using mysql 8.0.21
Any help to make this query work would be greatly appreciated.
Thank you
EDIT 1:
What I am trying to achieve is to update the gpid column with row numbers instead of the actual AUTO_INCREMENT id, which contains gaps in between ids, the explanation in this post Change mysql auto increment id column value
The UPDATE... FROM ... syntax is not supported by MySql.
You can do it with a JOIN:
UPDATE gp
INNER JOIN (
SELECT gpid, ROW_NUMBER() OVER (ORDER BY gpid) AS new_gpid
FROM gp
) t ON t.gpid = gp.gpid
SET gp.gpid = t.new_gpid;
See a simplified demo.
Your nested query is selecting from 'gp' but is then aliased as 'gp' it can't select from itself.
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".
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 am working on this SQLFiddle and cant get the command working. Here the command:
SET #n := 1;
SET #start := '2013-07-22 10:00:01';
SET #end := '2013-07-22 10:00:02';
SET #register := 40001;
SELECT * FROM
(
SELECT
`realvalues`.`Timestamp`,
`realvalues`.`Value` * `register`.`Factor`,
#x := #x + 1 AS rank
FROM
`realvalues`,
(SELECT #x := 0) t
WHERE
`realvalues`.`Register` = register AND
`realvalues`.`Timestamp` BETWEEN start AND end
JOIN
`register`
ON
`register`.`DeviceID` = `realvalues`.`DeviceID` AND
`register`.`Register` = `realvalues`.`Register`
) a
WHERE
rank MOD ? = n
Does anybody know where the command fails? MySQL error reporting isnt very usefull.
[EDIT] The Value is now duplicated with Factor.
There are many many things wrong with your query. However, the error that is being reported in your fiddle is:
...check the manual that corresponds to your MySQL server version for the right syntax to use near 'BETWEEN start AND end JOIN register ON ...
Your syntax for BETWEEN is incorrect. There should be no IS token before BETWEEN. Correct syntax is:
<value> BETWEEN <lower-bound-inclusive> AND <upper-bound-inclusive>
Other problems include:
start, end, and n are not columns
register (in the WHERE clause) is ambiguous
You have a JOIN clause after a WHERE clause
You do not specify an alias for the second column of your derived table a (perhaps not necessary but may cause issues)
Use of a ? parameter without a way to specify a value (although this is a limitation of SQL Fiddle and not necessarily a problem with your SQL statement)
i dont see usage of #start and #end
EDIT: now it works !
sqlfiddle.com/#!2/6dc97/50
I want to update the "rank" for a group of MySQL records with sequential numbers using a user-defined variable. The following query runs fine via the MySQL command line:
SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC
But if I try and run it as a Fluent query using Laravel, it fails.
DB::query("SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC");
Error message:
SQLSTATE[42000]: Syntax error or access violation: 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 'UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY' at line 1
SQL: SET #rank:=0; UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC
Bindings: array (
)
[SOLVED]
DB::raw() to the rescue! The following works:
DB::query(DB::raw("SET #rank:=0"));
DB::query("UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=4 ORDER BY score DESC");
It is not possible to execute multiple statements in one query. Laravel uses PDO under the hood which prevents this. You could attempt to call this over 2 queries instead, since #rank should be available for the duration of the connection.
DB::query("SET #rank:=0");
DB::query("UPDATE scores SET rank=#rank:=#rank+1 WHERE game_id=? ORDER BY score DESC", array(4));