0
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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The "having" clause solved my problem. How do I acknowledge this? This is my first time to use this forum.
@JimSawyer You can read this: meta.stackoverflow.com/questions/251078/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.