Solved

Calulated Field?

Posted on 1999-01-09
13
402 Views
Last Modified: 2010-04-06
Can I use a Calulated Field to Sum the entire column in my Table and if so, how?  Will this slow things down for a large table?
0
Comment
Question by:gammill
[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
13 Comments
 
LVL 1

Accepted Solution

by:
adeng earned 50 total points
ID: 1355566
I've made this function in my own program to sum the entire column, but i didn't uses calculated field to store the result, because it's make the process very slow and unefficient, except it's a master-detail table, i put the result in a calculated field in master table.

function TDataModule1.Total(Tabel: TDataSet; Field: tField): Extended;
var
     Total         : Extended;
     PrevRecord    : TBookmark;
begin
     PrevRecord := Tabel.GetBookmark;
     try
          Tabel.DisableControls;
          Tabel.First;
          Total := 0;
         
          while not Tabel.EOF do
          begin
               Total := Total + Field.Value;
               Tabel.Next;
          end;
          Result := Total;
         
     finally
          Tabel.EnableControls;
          if PrevRecord <> nil then
          begin
               Tabel.GoToBookmark(PrevRecord);
               Tabel.FreeBookmark(PrevRecord);
          end;
     end;
end;

{on master table on calc event }
procedure TDataModule1.Table1.CalcFields(DataSet: TDataSet);
begin
     Table1TOTAL.Value:= Total(Table2, Table2TOTAL));
end;

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355567
hi gammil,

it can be also done with a query, like

select sum(YourFieldname) as Total from Yourtable

after query.open

you can say

YourTotalContentField := Query1.FieldbyName('Total').AsFloat;

in Code

query1.SQL.Clear;
query1.SQL.Add('select sum(YourFieldname) as Total from Yourtable');
query1.Open;
YourTotalContentField := Query1.FieldbyName('Total').AsFloat;
query1.Close;

The perfomance depends on the Database in background. SQL-Databases like ORACLE are very fast, PARADOX is slower in this case.

meikl
0
 

Author Comment

by:gammill
ID: 1355568
Thanks for the help adeng!  I'll give it a try.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:merry_prince
ID: 8149835
Hello kretzschmar and adeng,

Can you give me a sample about OnCalcFields event? How to show detailed content of ftMeme field by it? (Please note, not create new column. but use original column)

Please advise. It's urgent. Thanks a lot.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8149911
merry,

in the oncalcevent you can't use
directly a blob-field from the original dataset

but you could attach a calculated string- or memofield,
and fill it with a query in the onCalcField-event

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8149965
kretzschmar,

Can you give a sample? Thks.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8150037
a sample,
using the table custoly in the alias dbdemos

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBTables, DB, Grids, DBGrids;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1CustNo: TAutoIncField;
    Table1Last_Name: TStringField;
    Table1First_Name: TStringField;
    Table1VIP_Status: TStringField;
    Table1Address1: TStringField;
    Table1Address2: TStringField;
    Table1City: TStringField;
    Table1StateProv: TStringField;
    Table1Post_Code: TStringField;
    Table1Country: TStringField;
    Table1Phone: TStringField;
    Table1Fax: TStringField;
    Table1EMail: TStringField;
    Table1Remarks: TMemoField;
    Table1AMemoAsCalcField: TStringField;
    Query1: TQuery;
    procedure Table1CalcFields(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
  query1.Close;
  query1.ParamByName('ID').AsInteger := Table1.FieldByName('CustNo').AsInteger;
  query1.Open;
  Table1.FieldByName('AMemoAsCalcField').AsString :=
    TBlobField(query1.FieldByName('Remarks')).AsString;
  query1.Close;
end;

end.

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8156081
Hi meikl,

Thanks for your helps. If it's convenient for you. Can you provide the method "DrawCell" to show memo field?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8156943
? what does you mean ?
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8157099
I want to use OnDrawDataCell event of DBGrid to show memo field. I had know how to do it. Thanks a lot. If you would like to know the details. Please inform me.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157393
well,

applied to the sample above
(property defaultdraw of the dbgrid must be set to false)

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
  if field.FieldName = 'Remarks' then
  begin
    //show the Memo content in red
    DBGrid1.Canvas.Font.Color := clRed;
    DBGrid1.Canvas.TextRect(rect,rect.left + 2,rect.Top+2,Table1Remarks.AsString);
  end else
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

hope that is it

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8157419
meikl,

Ho, you are excellent. Keep in touch. Thank you very much.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157495
well, glad that it works for you ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 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