检查这段mysql语法错误set @n1= ceiling(0.25*(select count(*) from customers)); select @n1; set @n2=ceiling(0.8*(select count(*) from customers)); select @n2; with tmp as( select customerid,sum(amount) amt from customers join orders using(customerid) join orderitems using(orderid) group by customerid),tmp2 as( select *,rank() over(order by amt desc) num from tmp),tmp3 as( select *, case when num<@n1 then'A' when num>@n2 then'B' else 'C' end as'category' from tmp2) select *,row_number()over( partition by category order by amt desc) num1 from tmp3;
时间: 2024-02-23 15:03:00 浏览: 107
这段 MySQL 语法的确存在语法错误,是因为在 `tmp3` 子查询中给 `case` 语句的第二个 `when` 子句加了引号,导致 MySQL 无法正确解析该语句。正确的语法如下所示:
```
set @n1= ceiling(0.25*(select count(*) from customers));
select @n1;
set @n2=ceiling(0.8*(select count(*) from customers));
select @n2;
with tmp as(
select customerid,sum(amount) amt
from customers join orders using(customerid) join orderitems using(orderid)
group by customerid
),
tmp2 as(
select *,rank() over(order by amt desc) num
from tmp
),
tmp3 as(
select *,
case
when num<@n1 then 'A'
when num>@n2 then 'B'
else 'C'
end as category
from tmp2
)
select *,row_number()over( partition by category order by amt desc) num1
from tmp3;
```
注意,上面的 SQL 语句可能并不适用于所有版本的 MySQL 数据库,具体实现上可能会有所不同。
阅读全文