Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Cannot Create View with SET variable inside

Tags:

sql

mysql

I am trying to create a view with SET @rank = 0; inside but it's giving me errors. Been trying different things but it's not working. Can anyone please point me to the right direction?

CREATE VIEW S1_Bottom_Performer_AHT as (
SET @rank=0
SELECT @rank := @rank+1 AS '#',
                ei.SM,
                ei.TM,
                es.Month_Date,
                ei.emp_id,
                ei.DNAME,
                ei.STATUS,
                ei.SHIFT,
                ei.SKILL,
                ei.HIRE_DATE,
                ifnull(TIMESTAMPDIFF(MONTH, ei.HIRE_DATE, now()), '-') AS Tenure,
                ifnull(es.Call_Handled, '-') AS Call_Handled,
                ifnull(es.AHT, '-') AS AHT
FROM mtl_extended_info ei
LEFT OUTER JOIN
  ( SELECT es.Employee_ID,
           es.Month_Date,
           sum(es.Calls_Handled_Ct) AS Call_Handled,
           round((sum(es.I_Talk_Time_Sec) + sum(es.Hold_Time_Sec) + sum(es.I_Work_Time_Sec) + sum(es.I_AUX_Out_Time_Sec)) / sum(es.Calls_Handled_Ct)) AS AHT
   FROM cdl_agent_call_voume_gen es
   WHERE es.Month_Date = '2013-09-01'
   GROUP BY es.Employee_ID,
            es.Month_Date ) es ON es.Employee_ID = ei.emp_id
WHERE es.Month_Date = '2013-09-01'
  AND ei.Visible = 1
  AND ei.SKILL != 'RSD'
GROUP BY ei.emp_id
ORDER BY es.AHT DESC LIMIT 80);

Error message:

#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 'SET @rank=0
SELECT @rank := @rank+1 AS '#',
                ei.SM,
          ' at line 2 
like image 816
John Guan Avatar asked Sep 26 '13 17:09

John Guan


People also ask

What does := mean in MySQL?

Description. := Assign a value. = Assign a value (as part of a SET statement, or as part of the SET clause in an UPDATE statement)

How do you set a variable in MySQL query?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

How do I permanently set a global variable in MySQL?

To persist a global system variable to the mysqld-auto. cnf option file in the data directory, precede the variable name by the PERSIST keyword or the @@PERSIST. qualifier: SET PERSIST max_connections = 1000; SET @@PERSIST.

How do I create a new view in MySQL?

The basic syntax for creating a view in MySQL is as follows: CREATE VIEW [db_name.] view_name [(column_list)] AS select-statement; [db_name.] is the name of the database where your view will be created; if not specified, the view will be created in the current database.


2 Answers

I think you cannot do that.

As from the MYSQL guidelines:

A view definition is subject to the following restrictions:

[ deletia ]

The SELECT statement cannot refer to system or user variables.

like image 94
Rahul Tripathi Avatar answered Oct 25 '22 16:10

Rahul Tripathi


Views are select statements, nothing more. Views can not be multiple statements. If you can't get this view down to a single statement, try the suggestion here to use a function or procedure.

Try this suggestion for MYSQL using a join instead of a set.

JOIN    (SELECT @rank:= 0) r;

Here is a untested example, I am not sure if you can order by '#' at the end, but if you can it would sort everything correctly by rank:

CREATE VIEW S1_Bottom_Performer_AHT as (
SELECT @rank := @rank+1 AS '#', *
FROM
(SELECT         ei.SM,
                ei.TM,
                es.Month_Date,
                ei.emp_id,
                ei.DNAME,
                ei.STATUS,
                ei.SHIFT,
                ei.SKILL,
                ei.HIRE_DATE,
                ifnull(TIMESTAMPDIFF(MONTH, ei.HIRE_DATE, now()), '-') AS Tenure,
                ifnull(es.Call_Handled, '-') AS Call_Handled,
                ifnull(es.AHT, '-') AS AHT
FROM mtl_extended_info ei
LEFT OUTER JOIN
  ( SELECT es.Employee_ID,
           es.Month_Date,
           sum(es.Calls_Handled_Ct) AS Call_Handled,
           round((sum(es.I_Talk_Time_Sec) + sum(es.Hold_Time_Sec) + sum(es.I_Work_Time_Sec) + sum(es.I_AUX_Out_Time_Sec)) / sum(es.Calls_Handled_Ct)) AS AHT
   FROM cdl_agent_call_voume_gen es
   WHERE es.Month_Date = '2013-09-01'
   GROUP BY es.Employee_ID,
            es.Month_Date ) es ON es.Employee_ID = ei.emp_id
WHERE es.Month_Date = '2013-09-01'
  AND ei.Visible = 1
  AND ei.SKILL != 'RSD'
GROUP BY ei.emp_id
ORDER BY es.AHT DESC LIMIT 80)
) AS RESULTS
JOIN    (SELECT @rank:= 0) r; 
ORDER BY '#'
like image 27
Vulcronos Avatar answered Oct 25 '22 18:10

Vulcronos