9

我正在尝试用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);

错误信息:

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

2 回答 2

8

我认为你不能那样做。

从 MYSQL指南

视图定义受以下限制:

[删除]

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

于 2013-09-26T17:53:16.440 回答
5

视图是选择语句,仅此而已。视图不能是多个语句。如果您无法将此视图归结为单个语句,请尝试此处的建议以使用函数或过程。

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

JOIN    (SELECT @rank:= 0) r;

这是一个未经测试的示例,我不确定您是否可以在最后按“#”排序,但如果可以的话,它会按排名正确排序:

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 '#'
于 2013-09-26T18:01:42.267 回答