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)? It currently includes all clients.
Thanks, Spyke
You need a HAVING
clause:
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.