我有以下查询生成“无效使用组功能”错误:
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;
似乎无法弄明白。非常感谢任何帮助。
答案 0 :(得分:0)
您不能在WHERE子句中使用像SUM()
这样的聚合函数。聚合取决于所选的行,因此在选择行时使用它们没有意义。您可以在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;