SELECT shi,Xian,Name,XieYBH,ShuL,GuoJBTE,ShengBTE,sale,mode_sale,FactoryName,BusinessName,ChanPMC,JiJXH,GouJRQ FROM(tmp_apply t1 JOIN ( SELECT FactoryName, ChanPMC, JiJXH, sale AS mode_sale FROM ( SELECT FactoryName, ChanPMC, JiJXH, sale, ROW_NUMBER() OVER (PARTITION BY FactoryName, ChanPMC, JiJXH ORDER BY COUNT(*) DESC) AS rn FROM tmp_apply GROUP BY FactoryName, ChanPMC, JiJXH, sale ) t WHERE rn = 1) t2 ON t1.FactoryName=t1.FactoryName AND t1.ChanPMC=t2.ChanPMC AND t1.JiJXH=t2.JiJXH WHERE ABS(sale-mode_sale)>5; 提示> [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]关键字 'WHERE' 附近有语法错误。
时间: 2024-01-28 20:04:54 浏览: 55
这条 SQL 查询语句存在语法错误,问题在于 `FROM` 关键字后面的表格名 `tmp_apply` 应该与括号之间留一个空格,即应该写成 `FROM (tmp_apply t1 JOIN ...`。因此,正确的查询语句应该是:
```
SELECT shi,Xian,Name,XieYBH,ShuL,GuoJBTE,ShengBTE,sale,mode_sale,FactoryName,BusinessName,ChanPMC,JiJXH,GouJRQ
FROM (tmp_apply t1 JOIN (
SELECT FactoryName, ChanPMC, JiJXH, sale AS mode_sale
FROM (
SELECT FactoryName, ChanPMC, JiJXH, sale,
ROW_NUMBER() OVER (PARTITION BY FactoryName, ChanPMC, JiJXH ORDER BY COUNT(*) DESC) AS rn
FROM tmp_apply
GROUP BY FactoryName, ChanPMC, JiJXH, sale
) t
WHERE rn = 1
) t2
ON t1.FactoryName=t2.FactoryName AND t1.ChanPMC=t2.ChanPMC AND t1.JiJXH=t2.JiJXH)
WHERE ABS(sale-mode_sale)>5;
```
你可以将这个修正后的查询语句尝试运行,看看是否能够正常执行。
阅读全文