Error:View's SELECT contains a variable or parameter - mysql

I am using MySQL work bench 8 :
I am not able to create this view as I am getting following error:
View's SELECT contains a variable or parameter
Here is my view:
Create view history as
select ShippedDate, round(previous_operation) as DayEnd, DayStart ,Reorderunits,Quantity,reorderlevel from (
select
y.*
, #prev AS previous_Operation
, #prev := DayStart
from
ExpectedHistory y
, (select #prev:=NULL) vars
order by ShippedDate desc
Note : #prev holds an integer value

If you want the previous operation in MySQL 8+, use lead():
create view history as
select ShippedDate, DayStart,
lead(day_start) over (partition by shippeddate) as as dayend,
Reorderunits, Quantity, reorderlevel,
from ExpectedHistory eh;

Related

alternate of group_concat function in mysql

I need some comma separated values for one of my column :
Input :
id status
1. pause
1. start
1. running
2. pause
3. pause
Output:
id. status
1. pause,start,running
2. pause
3. pause
I do not want to use the group_concat function here as in future in it can be possible that will be using another type of database (sql/nosql).Can anyone help me to use any alternate method?
WITH RECURSIVE
cte1 AS (
SELECT id, status,
ROW_NUMBER() OVER (PARTITION BY id) rn
FROM test
),
cte2 AS (
SELECT id, rn,
CAST(status AS CHAR(65535)) csv
FROM cte1
WHERE rn = 1
UNION ALL
SELECT cte1.id, cte1.rn,
CONCAT_WS(',', cte2.csv, cte1.status)
FROM cte2
JOIN cte1 ON cte1.id = cte2.id AND cte1.rn = cte2.rn + 1
),
cte3 AS (
SELECT id, csv,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY rn DESC) rn
FROM cte2
)
SELECT id, csv
FROM cte3
WHERE rn = 1
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0713abe3711c98c91dd1ceadb533e968
Another DBMSs needs minimal changes when adapting this query. Removing RECURSIVE from WITH clause definition, replacing CONCAT_WS() with according function (or maybe using common concatenation if statusis not nullable), removing explicit CAST in recursive CTE or changing the datatype...
PS. Another DBMSs have the function which is an analog of GROUP_CONCAT() in MySQL, or at least they have some specific constructions/methods for to perform such task.

MySQL: user-variable definition within SQL statement to create counter column

Is it possible to create a counter in mysql/mariadb in one single SELECT-statement. I've tried the following but it returns only the value 1 in the first column:
SELECT #rownr := IF(ISNULL(#rownr),0,#rownr)+1 AS rowNumber, * FROM table_x LIMIT 0,10
If I run the statement more often in the same mysql-instance it starts counting from the last number. So the second time it starts at 2, the third time at 12. This means that the variable is created but seems to be only available for modification when it was instantiated before the SQL statement was issued.
It is possible, but a bit tricky. First, you need to declare the variable outside of the select clause (in a separate set assignment, or in a derived table). Also, it is safer to sort the rows in a subquery first, and then compute the variable.
I would recommend:
set #rn := 0;
select t.*, #rn := #rn + 1 rowNumber
from (select t.* from mytable t order by id limit 10) t
Note that I added an order by clause to the inner query, otherwise it is undefined in which sequence rows will be ordered (I assumed id).
Alternatively, you can declare the variable in a derived table:
select t.*, #rn := #rn + 1 rowNumber
from (select t.* from mytable t order by id limit 10) t
cross join (select #rn := 0) x
Finally: if you are running MySQL 8.0, just use row_number():
select t.*, row_number() over(order by id) rn
from mytable t
order by id
limit 10;
You don't have an order by, so the ordering is indeterminate. But you can initialize the parameter in the statement itself:
SELECT #rownr := (#rownr + 1) AS rowNumber, x.*
FROM table_x x.CROSS JOIN
(SELECT #rownr := 0) params
LIMIT 0, 10;
If you want a particular ordering, you should use an order by in a subquery.
Also note that starting in MySQL 8, variable assignments in SELECT are deprecated. You should be using window functions (row_number()) in more recent versions.

RANK query using PARTITION BY fails in mysql

I tried below query to partition by but which fails with below query, the inner query works
select issueid, task_type, assignee, timeoriginalestimate, CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rank
from(
--- Complex query with p.pname, i.issuenum, cg.issueid, it.pname task_type, i.assignee, i.timeoriginalestimate, cg.CREATED, columns which works fine
)
Exception:
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 issueid order by CREATED desc ) as rank
from(
SELECT p.pna' at line 3
Update:
SELECT VERSION(); -- 5.6.27
Although your MySQL version do not support Window function, I am letting this is not the issue. Guess you have a higher version and window function is supported.
Now, in your query you have defined the Alias of a column name to "Rank" which is a reserved keyword for your database and you can not use that as column name.
Hope this below hints will help you-
select
issueid,
task_type,
assignee,
timeoriginalestimate,
CREATED,
dense_rank() over ( partition by issueid order by CREATED desc ) as rn -- change the alias name
from(
-- Your subquery
) A -- Also need to give a Alias name to your sub query
Finally, if you have lower version check this LINK for help to get an idea of creating Row_number or Ranking for MySQL older versions.
In addition, this following sample query will really help you finding solution for different type row_number in mysql-
SET #simple_row_number := 0;
SET #id_wise_row_number := 0;
SET #dense_rank_per_id := 0;
SET #prev := 0;
SELECT *,
#simple_row_number := #simple_row_number + 1 AS simple_row_number,
#id_wise_row_number := IF(issueid > #prev, #id_wise_row_number + 1, #id_wise_row_number) AS id_wise_row_number,
#dense_rank_per_id :=IF(issueid > #prev,1, #dense_rank_per_id + 1) AS dense_rank_per_id,
#prev := A.issueid Prev_IssueId
FROM (
SELECT 1 issueid, '20200601' CREATED UNION ALL
SELECT 1 issueid, '20200401' CREATED UNION ALL
SELECT 1 issueid, '20200501' CREATED UNION ALL
SELECT 1 issueid, '20200201' CREATED UNION ALL
SELECT 1 issueid, '20200301' CREATED UNION ALL
SELECT 2 issueid, '20200301' CREATED UNION ALL
SELECT 2 issueid, '20200201' CREATED UNION ALL
SELECT 2 issueid, '20200401' CREATED
) A
ORDER BY issueid, CREATED DESC

The same SQL but have different result

ps:the description show detail with the href: https://segmentfault.com/q/1010000014649728
I run the follow sql :
select * from
(
select *,(#num2 :=
(if(#GROUP199=`C_ISHOT`,#num2+1,if(#GROUP199:=`C_ISHOT`,1,1)))) row_number
from city_code order by C_ISHOT
) result
where row_number<=10
the first run result show the table total numbers,
but the second run result is my expected。 what's the matter with it,and how can I resolve that?
The variables are not initialized.
select *
from ( select *, (#num2 :=(if(#GROUP199=C_ISHOT,#num2+1,if(#GROUP199:=C_ISHOT,1,1)))) row_number
from city_code, ( SELECT #num2 := 0, #GROUP199 := 0 ) init_vars
order by C_ISHOT ) result
where row_number<=10

MySQL – Generating Row Number for Each Row Using Variable

Oracle query is-
select ROW_NUMBER () OVER(order by shortunitdesc) as SLNO,
om.shortunitdesc,oc.operparam as Parameter,
oc.tagno,oc.severity,lowlimit,highlimit, oc.operrange, IMPLICATION, DURATION from oeconfig oc, oeunitmaster om
where oc.unitcode = om.unitcode;
I want to convert in mysql, so I have tried
select #i:=#i+1 as slno, om.shortunitdesc, oc.operparam as Parameter,
oc.tagno,oc.severity,oc.lowlimit,oc.highlimit,oc.operrange , IMPLICATION, DURATION
from oeconfig oc oeunitmaster om , (select #i := 0)
where oc.unitcode = om.unitcode
order by shortunitdesc;
but it gives an error- Every derived table must have its own alias
select #i:=#i+1 as slno, om.shortunitdesc, oc.operparam as Parameter,
oc.tagno,oc.severity,oc.lowlimit,oc.highlimit,oc.operrange , IMPLICATION, DURATION
from oeconfig oc, oeunitmaster om, (select #i := 0) t
where oc.unitcode = om.unitcode
order by shortunitdesc;