错误:您的SQL语法出现了错误;请检查与您的MySQL服务器版本对应的手册,以获得正确的语法使用near‘(按名称顺序按计数顺序划分)作为第4行的秩(选择名称,计数(Case_),在第4行)。
select Name, counts
from (
select Name, counts,
rank()over(partition by name order by counts desc) as rank
from (
select Name, count(Case_Id) as counts,
from CTD a
left join Agent_Table b
on a.Agent_ID = b.Agent_ID
group by Name )
group by Name )
where rank < = 2;创建表请参阅下面的内容
如果不存在Agent_Table,则创建表( Agent_ID int(5),Name varchar(20) );
插入Agent_Table (Agent_ID,Name)值( '1','Vijay'),( '2','Rajesh'),('3','Satish'),('4','Anji');
如果不存在CTD,则创建表( Case_Id int(5)、Stage varchar(20)、Login_Time varchar(50)、Logout_Time varchar(50)、Agent_ID int(5)、Status varchar(20) );
插入CTD (Case_Id,Stage,Login_Time,Logout_Time,Agent_ID,Status)值('101','Maker','5/11/2019 10:20','10:30','2',‘成功’),('102',‘制造者’,'5/11/2019 10:25','10:35','1',‘成功’),('103',‘制造者’,'5/11/2019 10:40','10:50','2',‘成功’),('101','Checker','5/11/2019 10:45','11:00','3',‘成功’),('101','Approver','5/11/2019 11:15','11:30','2',‘成功’),('102','Checker','5/11/2019 10:50,'11:00','1',‘拒绝’,('102',‘制造者’,'5/11/2019 11:15','11:45','4',‘崇敬’),('103','Checker','5/11/2019 11:30','11:40','2',‘拒绝’)
发布于 2020-06-18 20:16:39
您没有为子查询提供alias,这可能是您收到错误的原因。
尝试以下几点
select
Name,
counts
from
(
select
Name,
counts,
rank() over(partition by Name order by counts desc) as rn
from
(
select
Name,
count(Case_Id) as counts
from CTD a
left join Agent_Table b
on a.Agent_ID = b.Agent_ID
group by
Name
) subq
) sub
where rn <= 2;发布于 2020-06-18 20:33:12
你有一个不必要的逗号,没有必要的别名。在任何情况下,窗口函数都是在group by之后应用的,因此实际上可以将查询简化为
select Name,
counts
from (select Name,
count(Case_Id) as counts,
rank() over (partition by name order by count(Case_Id) desc) as rnk
from CTD a
left join Agent_Table b on a.Agent_ID = b.Agent_ID
group by Name) t1
where rank < = 2;https://stackoverflow.com/questions/62458575
复制相似问题