Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using SQL To Find Sum

Posted on 1999-07-12
7
Medium Priority
?
182 Views
Last Modified: 2011-09-20
I have a form that I dropped a TQuery (call it Query1) onto.  I need Query1 to give me the total number of records that meet the certain conditions.  The data is stored in a TTable (call it Table1).  The conditions are:
myId = x    // x can change
myWt = y    // y can change

What would I enter for Query1.Params and Query1.SQL?  What would I type in order to use the result of this query, i.e. total number of records that meet my conditions e.g. ttlRecords := ??? ?  What other things (highly technical term) do I need to set to make the query work?
0
Comment
Question by:tecbuilder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Author Comment

by:tecbuilder
ID: 1387915
Edited text of question.
0
 
LVL 6

Author Comment

by:tecbuilder
ID: 1387916
Adjusted points to 150
0
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 600 total points
ID: 1387917
In your query:

Select COUNT(field in table) from TABLE where blah blah blah.

Should return the total number of records that meet this criteria.

Good luck with your project!

Pegasus
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1387918
Tecbuilder, reread your question, try this for your SQL.Text:

Select Count(*) from TABLENAME where
myId = :myIdVariable AND
myWt = :myWtVariable

Make sure the query is closed then:

Query1.ParamByName('myIdVariable').AsInteger := 50;
Query1.ParamByName('myWtVariable').AsInteger := 50;
Query1.Open;
yourrecordcount := Query1.Fields[0].AsInteger;

Good luck with your project!
Pegasus
0
 
LVL 6

Author Comment

by:tecbuilder
ID: 1387919
Should I use a try - finally block around any of this? If so where should it start and what should go in the finally part?

Thanks for your quick response!
0
 
LVL 3

Expert Comment

by:Pegasus100397
ID: 1387920
TecBuilder,

   You *could* put a try..finally block around it (and it's good programming practice, although I would use the Try..EXCEPT statement instead ;) so I'd re-write it like this if you wanted to handle a possible exception:

Query1.ParamByName('myIdVariable').AsInteger := 50;
Query1.ParamByName('myWtVariable').AsInteger := 50;
Try
 Begin
  Query1.Open;
  yourrecordcount := Query1.Fields[0].AsInteger;
 end;
Except
  begin
  ShowMessage('Query OPEN failed! Help!');
  yourrecordcount := 0;
  end;
end;

Good luck and let me know how it works out!
Pegasus
0
 
LVL 6

Author Comment

by:tecbuilder
ID: 1387921
Thanks for your help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question