Aggregates / Sum of a Field of all Records

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
dschuch
Posts: 75
Joined: Thu 05 Feb 2009 15:29
Location: Dresden

Aggregates / Sum of a Field of all Records

Post by dschuch » Wed 13 Feb 2013 13:40

Hi,

like descriped here (and in the help) Aggregate Fields arent supported.
http://forums.devart.com/viewtopic.php? ... eld#p61550

So what is the best way, if you need the sum of a field of all records of a dataset?

is there sth around using "DisableControls, First, Next" etc? I do not want to use this way, because events like afterscroll are fired, etc.

currently my code looks like:

Code: Select all

 R:=RecNo;
 Result:=0;
 DisableControls;
 S:=AfterScroll;
 AfterScroll:=Nil;
 First;
 try
  while not EoF do begin
           Result:=Result+FieldByName(SumFieldName).AsFloat;
        Next;
  End;
 finally
  RecNo:=R;
  AfterScroll:=S;
  EnableControls;
 end;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Aggregates / Sum of a Field of all Records

Post by AlexP » Wed 13 Feb 2013 15:12

Hello,

Aggregate fields are not supported either in UniDAC or in PgDAC. To retrieve the sum by a field, you can use window functions of the PostgreSQL server. For example, the following query will return all the records from the EMP table and the common sum by the SAL field:

Code: Select all

SELECT empno, ename, sal, sum(sal) OVER () FROM emp
You can find more detailed information on window functions in the PostgreSQL documentation ( http://www.postgresql.org/docs/9.2/stat ... indow.html )

dschuch
Posts: 75
Joined: Thu 05 Feb 2009 15:29
Location: Dresden

Re: Aggregates / Sum of a Field of all Records

Post by dschuch » Wed 13 Feb 2013 17:01

hi, thanks, yes - using this very often.

but e.g. if you need to sum a calculated field you have to loop over your dataset. (or a client filter) so perhaps there is a way to use some kind of internal "hack" to loop over the row values without using the standard way (next/prior).

e.g. in DevExpress i can get a rowValue with the currentRow or via

DataController.Values[RecordNumber, Field]

so i can get my result [run through all records] without moving the current record (next/prior), so i do not have to look for side effects of the frontend (scrolling, enable/disable controls etc). i can just run over all records in a simple loop.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Aggregates / Sum of a Field of all Records

Post by AlexP » Thu 14 Feb 2013 13:39

Hello,

To retrieve the field value, you should move to a needed record, therefore, to retrieve all values of a field, you should run through all records of DataSet. We didn't deal with internal behaviour of DevExpress components, however, to get the behaviour you have described, most probably, DevExpress also uses the DisableControls and EnableControls methods to move to a needed record not displaying scrolling. Otherwise, DevExpress can have its own cache for data storing and access, from which data is retrieved by the methods you have described.

dschuch
Posts: 75
Joined: Thu 05 Feb 2009 15:29
Location: Dresden

Re: Aggregates / Sum of a Field of all Records

Post by dschuch » Tue 05 Mar 2013 07:13


AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Aggregates / Sum of a Field of all Records

Post by AlexP » Tue 05 Mar 2013 11:56

Hello,

Yes we plan to implement this feature for both TVirtualTable and other our DataSets, however, we cannot nominate the exact terms of the implementation.

Post Reply