Solved

get total in Query

Posted on 2002-04-03
10
142 Views
Last Modified: 2010-04-04
here is some code,
HOW CAN I GET THE TOTAL OF CR and DB?

GLQuery.Close;
GLQuery.SQL.Clear;
GLQuery.SQL.Add('Select E.AccntDate, C.GLAccount, C.AccountName, SUM(E.Debit) as DB, SUM(E.Credit) as CR FROM '+file_ChartofAccount +' C LEFT OUTER JOIN '+file_JournalEntry+' E ON (C.GLAccount = E.GLAccount) ');
GLQuery.SQL.Add('Where E.BatchNumber = '+QuotedStr(Alltrim(wwDBComboDlg1.text)));
GLQuery.SQL.Add('GROUP BY E.AccntDate, C.GLAccount, C.AccountName, E.AccntDate');
GLQuery.SQL.Add('ORDER BY E.AccntDate');
GLQuery.OPEN;
0
Comment
Question by:girlswants_me
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 6917025
SUM(E.Debit) as DB, SUM(E.Credit) as
CR , SUM(E.Debit + E.Credit) as SUM_CRDB
0
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 6917318
If you want the grand totals you need another query or add them in your code:

Select
  SUM(E.Debit) as DB,
  SUM(E.Credit) as CR
FROM
  '+file_ChartofAccount +' C LEFT OUTER JOIN
  '+file_JournalEntry+' E ON (C.GLAccount = E.GLAccount)
Where E.BatchNumber =
  '+QuotedStr(Alltrim(wwDBComboDlg1.text))

just leave group by and order by away. You'll get one record as a result then.

Regards Jacco
0
 

Author Comment

by:girlswants_me
ID: 6919893
i mean if its already QUERIED OR FILTERED in that
Query Module, how can i Total all Credit or Debit
in that query, by not calling another query to do the total?

am i going to TRAVERSE from the FIRST record down to the LAST record and ADD every RECORD encountered?

PLEASE give me some code for it by not affecting the Query
while traversing from first to last.coz when i traverse the
query my Grid also moves.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 45

Expert Comment

by:aikimark
ID: 6920029
I think it is time for you to more fully state all the parameters and requirements of your problem.
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6920144
It might be better to calculate the totals before you connect the dataset to the datasource.

But here is some code to do what you want. The same record will be selected, but the position in de grid might change a bit.

var
  DBTotal, CRTotal: Extended;  
  lbRestore: Boolean;
  lpBookMark: Pointer;
begin
  DBTotal := 0;
  CRTotal := 0;
  with GLQuery do
  begin
    lbRestore := False;
    if DBGrid1.SelectedRows.Count > 0 then
    begin
      lbRestore := True;
      lpBookMark := Pointer(DBGrid1.SelectedRows.Items[0]);
    end;
    DisableControls;
    try
      while not EOF do
      begin
        DBTotal := DBTotal + FieldByName('DB').AsFloat;
        CRTotal := CRTotal + FieldByName('CR').AsFloat;
      end;
      if lbRestore then
      begin
        GotoBookmark(lpBookMark);
      end;
    finally
      EnableControls;
    end;
  end;
end;

This code uses a bookmark to restore the currently selected record. And DisableControls will have the grid frozen until you have your totals.

I'm afraid this is the best you can do.

Regards Jacco
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6920245
Jacco,
I would agree, but it seems that the user might be able to do some filtering of the rows, which would invalidate the original sum.  That's why I asked for a more accurate statement (scope) of the problem.  This started as a SQL question and we are now getting some Delphi constraints.
0
 
LVL 4

Expert Comment

by:SurferJoe
ID: 6922936
What DataBase are you using?

Stored procedures work great for this sort of thing,
in SQL dialects.

I can provide samples in Transact SQL or InterBase, if useful?

G
0
 
LVL 10

Expert Comment

by:Jacco
ID: 6922976
After a filter change you can recalculate the totals.

Regards Jacco
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6923007
That's why I recommended that the question be restated with all constraints given to the experts.
0
 

Author Comment

by:girlswants_me
ID: 6927521
thank you , sorry for the long time grade
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 135
add combobox item based on numbers 9 141
how to center only a line in richedit? 4 56
error 1.1 400 Bad request idhttp delphi 18 86
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

785 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