MySQL - 无法在里面用SET变量创建视图

时间:2022-09-24 23:29:32

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?

我试图用SET @rank = 0创建一个视图;在里面,但它给了我错误。一直尝试不同的东西,但它不起作用。谁能指点我正确的方向?

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 

2 个解决方案

#1


6  

I think you cannot do that.

我想你不能这样做。

As from the MYSQL guidelines:

从MYSQL指南来看:

A view definition is subject to the following restrictions:

视图定义受以下限制:

[ deletia ]

[deletia]

The SELECT statement cannot refer to system or user variables.

SELECT语句不能引用系统或用户变量。

#2


5  

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.

使用连接而不是集合为MYSQL尝试此建议。

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 '#'

#1


6  

I think you cannot do that.

我想你不能这样做。

As from the MYSQL guidelines:

从MYSQL指南来看:

A view definition is subject to the following restrictions:

视图定义受以下限制:

[ deletia ]

[deletia]

The SELECT statement cannot refer to system or user variables.

SELECT语句不能引用系统或用户变量。

#2


5  

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.

使用连接而不是集合为MYSQL尝试此建议。

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 '#'