MySQL排名超过功能

时间:2018-07-31 15:24:22

标签: mysql

我在MySQL工作台中使用以下查询,并且在函数over函数附近出现错误。我认为MySQL可能不支持对函数的Rank()支持。因此,请任何人可以帮助成功运行此程序吗?

select STORE_CODE, 'GBP', "SALES WITHOUT VAT", 'UK' from(
select STORE_CODE, "SALES WITHOUT VAT", RANK() OVER(ORDER BY "SALES WITHOUT VAT" DESC) AS RANK 
 from( select store_code, sum("SALES WITHOUT VAT") as "SALES WITHOUT VAT" 
       from( select c_site AS STORE_CODE, case when c_currency = 'GBP' then A_SALES_EXVAT_REAL 
       when c_currency = 'EUR' then round(A_SALES_EXVAT_REAL * ukurs ,3) end as "SALES WITHOUT VAT" 
       from kf_move01.salesticket_line left outer join
       (select ukurs, to_date(99999999-gdatu), year(to_date(99999999-gdatu)) as year, month(to_date(99999999-gdatu)) as month 
       from "KF_MOVE01"."TCURR" 
       where mandt = 201 and kurst = 'M' and tcurr = 'GBP' and fcurr = 'EUR' 
       order by 99999999-gdatu desc) as tcur 
       on tcur.year = year(d_year_month_day) and tcur.month = month(d_year_month_day) 
       where id_opco = 2 and c_comp_code in ('BQ10', 'BQ20','BQ30','BQ40') 
       and d_year_month_day between add_days(now(),-181) and add_days(now(),-1)) 
       group by store_code 
       order by 2 desc ) ) where rank <= 5;
  

错误代码:1064。您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以获取正确的语法,以在第6行0.000秒处在'(ORDER BY“ SALES WITHAT VAT” DESC)FROM(SELECT')附近使用Java错误:SAP DBTech JDBC:[279](第179页) ):组函数嵌套:第1行第180行(位置179)

在线SQL检查器错误:

  

您的SQL语法有错误;似乎错误就在周围:'(在第10行的((按“不含增值税的销售”价格”订购)排名(('

1 个答案:

答案 0 :(得分:0)

如果您实际上使用的是MySQL 8.0,那么这里有几件事。

可以使用双引号"SALES WITHOUT VAT"括住列别名,但是从子查询中选择同一列时不能用双引号引用它。您必须使用反引号,否则MySQL会认为您实际上是在选择名为“ SALES WITHOUT VAT”的字符串。

为了使等级函数正常工作,请尝试将order by移至查询末尾。

select STORE_CODE, 'GBP', `SALES WITHOUT VAT`, 'UK' from(
select STORE_CODE, `SALES WITHOUT VAT`, RANK() over w AS `RANK`
.
.
.
) window w as (order by `SALES WITHOUT VAT` desc))
where `RANK` <=5;

玩弄这种SQL提琴可能会有所帮助。

https://www.db-fiddle.com/f/28ZF6xdoWsVvH6JTNsFdQo/3