Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

sql - MySQL - Cannot Create View with SET variable inside

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...