0

我有以下查询生成“组函数的无效使用”错误:

SELECT dac_name, unit_name, ptn, unit_type, monthly_recurring, directory_charges
FROM sprint WHERE date='$current_sprint_date' AND CAST( directory_charges AS DECIMAL( 10, 2 ) ) > SUM( CAST( directory_charges AS DECIMAL( 10, 2 ) ) * .01) ORDER BY CAST(directory_charges AS DECIMAL(10,2)) DESC LIMIT 10; 

似乎无法弄清楚。任何帮助是极大的赞赏。

4

1 回答 1

0

您不能像SUM()在 WHERE 子句中那样使用聚合函数。聚合取决于选择的行,因此在选择行时使用它们没有意义。您可以在子句中使用它,该HAVING子句在选择和处理行后执行第二个过滤器:

SELECT dac_name, unit_name, ptn, unit_type, monthly_recurring, directory_charges
FROM sprint
WHERE date='$current_sprint_date'
HAVING CAST( directory_charges AS DECIMAL( 10, 2 ) ) > SUM( CAST( directory_charges AS DECIMAL( 10, 2 ) ) * .01)
ORDER BY CAST(directory_charges AS DECIMAL(10,2)) DESC
LIMIT 10;
于 2013-06-21T00:23:16.360 回答