[英]Delphi, FireDAC, MySQL Query
qClient.SQL.Clear;
qClient.SQL.Text := 'Select C.Lastname,C.Firstname,C.Clino,C.Address,' +
'C.City,C.State,C.Zip,C.Phone,C.HerCell,C.HisCell,' +
'C.Texting,C.Since,C.Selected,' +
'SUM( I.Charges - I.Discount ) as Revenue ' +
'from Client C join Invoice I ' +
'on I.Clino = C.Clino ' +
'where I.Date between :S and :E ' +
'and I.Inv = true ' +
'group by C.Clino ' +
'order by Revenue desc ';
qClient.ParamByName('S').AsDate := dStart;
qClient.ParamByName('E').AsDate := dEnd;
qClient.DetailFields := 'Clino';
qClient.Open();
How can I modify the above code so that the query includes only 'Revenue' values over a minimum (like maybe 1000)?如何修改上面的代码,以便查询仅包含超过最小值(例如可能为 1000)的“收入”值? It currently includes all clients.
它目前包括所有客户端。
Thanks, Spyke谢谢,斯派克
You need a HAVING
clause:你需要一个
HAVING
子句:
qClient.SQL.Text := 'Select C.Lastname,C.Firstname,C.Clino,C.Address,' +
'C.City,C.State,C.Zip,C.Phone,C.HerCell,C.HisCell,' +
'C.Texting,C.Since,C.Selected,' +
'SUM( I.Charges - I.Discount ) as Revenue ' +
'from Client C join Invoice I ' +
'on I.Clino = C.Clino ' +
'where I.Date between :S and :E ' +
'and I.Inv = true ' +
'group by C.Clino ' +
'having Revenue > 1000 ' +
'order by Revenue desc ';
The HAVING
clause applies conditions and filters the results of the query after the aggregation. HAVING
子句应用条件并过滤聚合后的查询结果。
In this case Revenue
is an aggregated column and could not be used in a WHERE
clause which also filters a query, because the conditions of the WHERE
clause are applied before the aggregation.在这种情况下
Revenue
是聚集的柱,不能一个中使用WHERE
子句,其还过滤的查询,因为条件WHERE
聚集之前被施加子句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.