Top 80% query problem



I'm trying to get a list of customers who have the top 80% of sales.
I'm using a TADOQuery in Delphi 2006 to attempt to get the results.
What I'm trying to do is get a total of sales by walking the query
results once. Then I'm finding 80% of total sales and moving the
dataset back to the first record. I'm then walking the dataset,
counting down the top 80% total. Each record in the top 80% is getting
edited and posted. I'm doing this to add some additional information
to the query. After the top 80% of records is done, I'm deleting the
rest of the rows.

The problem I'm having is that I'm somehow ending up deleting all rows
in the dataset. The dataset should be ordered by sales. Am I hitting
the top 80% rows a second time because I'm updating them? Please help.
Thanks in advance.

Here is the code. I've simplified a bit to make it easier to read.

// visual control on a form
TADOQuery:qCUSSLS;

with qCUSSLS do
begin
DisableControls;
Close;
Prepared := False;
SQL.Clear;
// put into temporary table so additional columns can be added to
table
qry := 'select * into #temp from D_CUSSLS where RecordStatus =
''A''';
qry := qry + ';alter table #temp add CYTDBudget numeric NULL;'
qry := qry + 'select * from #temp';
qry := qry + ' order by CYSales DESC';
SQL.Add(qry);
Prepared := True;
Open;
First;
// accumulate total sales
Top80Tot := 0;
while not Eof do
begin
Top80Tot := Top80Tot + qCUSSLSCYSales.Value;
Next;
end;
First;
// get 80% of total sales
Top80Tot := Top80Tot * 0.8;
while not Eof do
begin
delflg := False;
if Top80Tot < 0 then
begin
// delete row if it's not in the top 80%
delflg := True;
Delete;
end else
Top80Tot := Top80Tot - qCUSSLSCYSales.Value;
if not delflg then
begin
Edit;
qCUSSLSCYTDBudget.Value := 'logic omitted';
end;
if not delflg then
begin
Post;
Next;
end;
end;
First;
end;

.



Relevant Pages

  • RE: Sort by Total
    ... "Eduardo" wrote: ... When you copy the formula down it will fill out with the customer name. ... column where the total sales are, unselect the totals so you will see only ...
    (microsoft.public.excel.misc)
  • RE: Sort by Total
    ... "MarkT" wrote: ... When you copy the formula down it will fill out with the customer name. ... column where the total sales are, unselect the totals so you will see only ...
    (microsoft.public.excel.misc)
  • Re: need help with this formula
    ... Joan wrote: ... Each worksheet shows the previous total sales and the current week's ... If total revenue is equal or under $700,000, then the multiplying ... H65 represents total sales to date. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: need help with this formula
    ... By the way Morrigan, I was unable to pick up the attachment. ... Each worksheet shows the previous total sales and the current week's ... If total revenue is equal or under $700,000, then the multiplying ... H65 represents total sales to date. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: need help with this formula
    ... Joan Wrote: ... Each worksheet shows the previous total sales and the current week's ... If total revenue is equal or under $700,000, then the multiplying ... H65 represents total sales to date. ...
    (microsoft.public.excel.worksheet.functions)