[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

get total in Query

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
girlswants_me
Asked:
girlswants_me
  • 4
  • 3
  • 2
  • +1
1 Solution
 
aikimarkCommented:
SUM(E.Debit) as DB, SUM(E.Credit) as
CR , SUM(E.Debit + E.Credit) as SUM_CRDB
0
 
JaccoCommented:
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
 
girlswants_meAuthor Commented:
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
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.

 
aikimarkCommented:
I think it is time for you to more fully state all the parameters and requirements of your problem.
0
 
JaccoCommented:
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
 
aikimarkCommented:
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
 
Greg RowlandSoftware Designer, SysDBA, WebMaster OwnerCommented:
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
 
JaccoCommented:
After a filter change you can recalculate the totals.

Regards Jacco
0
 
aikimarkCommented:
That's why I recommended that the question be restated with all constraints given to the experts.
0
 
girlswants_meAuthor Commented:
thank you , sorry for the long time grade
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now