Board index » delphi » preventing repeat records from showing

preventing repeat records from showing

Hi there,
    we have an interesting problem. I want to prevent certain records in a
dataset (TQuery) from showing up in a report. I also do not  want them to be
included in any sum calculations. Adding a field in the database to
distinguish these records is not really an option right now, so I'm trying a
coding solution. Do you guys have any ideas?  I did attempt putting a
conditional Next in the AfterScroll event of the TQuery object, but it
seemed to corrupt/mixup the dataset pointers because I was getting
unexpected results (wrong record skipped and another record repeated). My
theory was probably flawed because I was causing event recursion. Anyhow, do
you guys know how to do this? Any advice would be appreciated. Thank you,

    Steve Anderson
    steve.ander...@homemail.com

 

Re:preventing repeat records from showing


Try setting Query1.Filtered to True and implement the OnFilterRecord event
handler.

"Steve Anderson" <steve.ander...@homemail.com> schreef in bericht
news:8b6ask$7hr$1@mur2.odyssey.on.ca...

Quote
> Hi there,
>     we have an interesting problem. I want to prevent certain records in a
> dataset (TQuery) from showing up in a report. I also do not  want them to
be
> included in any sum calculations. Adding a field in the database to
> distinguish these records is not really an option right now, so I'm trying
a
> coding solution. Do you guys have any ideas?  I did attempt putting a
> conditional Next in the AfterScroll event of the TQuery object, but it
> seemed to corrupt/mixup the dataset pointers because I was getting
> unexpected results (wrong record skipped and another record repeated). My
> theory was probably flawed because I was causing event recursion. Anyhow,
do
> you guys know how to do this? Any advice would be appreciated. Thank you,

>     Steve Anderson
>     steve.ander...@homemail.com

Re:preventing repeat records from showing


See 'SELECT DISTINCT'.  The bottom line is that you want to exclude them
from the query in the first place.

Paul / ColumbuSoft
www.columbusoft.com

Quote
Steve Anderson <steve.ander...@homemail.com> wrote in message

news:8b6ask$7hr$1@mur2.odyssey.on.ca...
Quote
> Hi there,
>     we have an interesting problem. I want to prevent certain records in a
> dataset (TQuery) from showing up in a report. I also do not  want them to
be
> included in any sum calculations. Adding a field in the database to
> distinguish these records is not really an option right now, so I'm trying
a
> coding solution. Do you guys have any ideas?  I did attempt putting a
> conditional Next in the AfterScroll event of the TQuery object, but it
> seemed to corrupt/mixup the dataset pointers because I was getting
> unexpected results (wrong record skipped and another record repeated). My
> theory was probably flawed because I was causing event recursion. Anyhow,
do
> you guys know how to do this? Any advice would be appreciated. Thank you,

>     Steve Anderson
>     steve.ander...@homemail.com

Re:preventing repeat records from showing


Hi there,
     Select Distinct will not work in this case, one field from a joined
table is different (ie/ the records are not entirely repeated, just
partially) and we need that value, we have tried Grouping By and Max() but
that doesn't work either. I could break up each joined table into a
different TQuery or TTable and then use DISTINCT, but that is a lot of work.
This report already has about 10 TQuery's. I know what your saying about not
bringing the data back in the first place, the extra field would be my
preferred sol'n, but then I'm just a lackey in this operation... thanks.

Quote
Paul Ferrara <p...@nospam.columbusoft.com> wrote in message

news:8b7jmf23be@news1.newsguy.com...
Quote
> See 'SELECT DISTINCT'.  The bottom line is that you want to exclude them
> from the query in the first place.

> Paul / ColumbuSoft
> www.columbusoft.com

> Steve Anderson <steve.ander...@homemail.com> wrote in message
> news:8b6ask$7hr$1@mur2.odyssey.on.ca...
> > Hi there,
> >     we have an interesting problem. I want to prevent certain records in
a
> > dataset (TQuery) from showing up in a report. I also do not  want them
to
> be
> > included in any sum calculations. Adding a field in the database to
> > distinguish these records is not really an option right now, so I'm
trying
> a
> > coding solution. Do you guys have any ideas?  I did attempt putting a
> > conditional Next in the AfterScroll event of the TQuery object, but it
> > seemed to corrupt/mixup the dataset pointers because I was getting
> > unexpected results (wrong record skipped and another record repeated).
My
> > theory was probably flawed because I was causing event recursion.
Anyhow,
> do
> > you guys know how to do this? Any advice would be appreciated. Thank
you,

> >     Steve Anderson
> >     steve.ander...@homemail.com

Re:preventing repeat records from showing


Thanks, for the detailed SQL info, I knew there was a way to do it  through
SQL without changing the db structure. Unfortunately I have already
implemented code through the OnFilter event to do the check, more bulky but
it works good. Anyhow I will keep your mini lesson for future reference.
Thanks everyone, I should have emailed this group 2 weeks ago... :o)

    Steve

Quote
Steve Koterski <koter...@NOSPAMgte.net> wrote in message

news:38d99305.1741134@news.gte.net...
Quote
> On Mon, 20 Mar 2000 18:07:43 -0500, "Steve Anderson"
> <steve.ander...@homemail.com> wrote:

> >    we have an interesting problem. I want to prevent certain records in
a
> >dataset (TQuery) from showing up in a report. I also do not  want them to
be
> >included in any sum calculations. Adding a field in the database to
> >distinguish these records is not really an option right now, so I'm
trying a
> >coding solution. Do you guys have any ideas?  I did attempt putting a
> >conditional Next in the AfterScroll event of the TQuery object, but it
> >seemed to corrupt/mixup the dataset pointers because I was getting
> >unexpected results (wrong record skipped and another record repeated). My
> >theory was probably flawed because I was causing event recursion. Anyhow,
do
> >you guys know how to do this? Any advice would be appreciated. Thank you,

> First you have to be able to define what makes a given row a duplicate
> or not a duplicate. Then you need to express that in SQL. Take the
> dataset below for example.

>   FirstName LastName DateIn
>   --------- -------- ----------
>   Joe       Smith    1/1/1999
>   Joe       Smith    3/1/2000
>   John      Jones    2/4/1999
>   Joe       Henry    5/6/1998
>   Brighton  Smith    12/31/1999
>   Mary      Jones    7/24/1997
>   Mary      Jones    2/1/2000

> There are two sets of duplicated records based on the combination of
> the FirstName and LastName fields: Joe Smith and Mary Jones. The
> difference between the record in each group is the value of the DateIn
> field. So this could be used to specify which is the duplicate (the
> ones in a group with less than the latest date) and which one to keep
> (the one with the latest date).

> A SELECT statement can use in its WHERE clause another SELECT acting
> as a subquery to define the criteria on which to restrict the effects
> of the data retrieval to a subset of the overall dataset. This
> situation requires two subqueries. The first is one for finding the
> groups where there are two or more records with the same FirstName and
> LastName. The second is one to determine the highest date for each
> group. When these two criteria are combined, they allow us to delete
> all but one record for each group of duplicates, the one with the
> latest date.

> The SELECT statement below retrieves all rows where there are two or
> more duplicates. Any records not included in this result set are those
> where they are the only record with a given combination of FirstName
> and LastName, i.e., not duplicates.

>   SELECT K.*
>   FROM "KillDupes.db" K
>   WHERE EXISTS
>     (
>     SELECT K1.LastName, K1.FirstName, COUNT(K1.DateIn)
>     FROM "KillDupes.db" K1
>     WHERE (K.LastName = K1.LastName) AND
>       (K.FirstName = K1.FirstName)
>     GROUP BY K1.LastName, K1.FirstName
>     HAVING (COUNT(K1.DateIn) > 1)
>     )

> This statement retrieves the records with other than the highest date
> for their respective groups (these groups being based on the
> combination of the LastName and FirstName fields).

>   SELECT K.*
>   FROM "KillDupes.db" K
>   WHERE NOT K.DateIn = ANY
>     (
>     SELECT MAX(K2.DateIn)
>     FROM "KillDupes.db" K2
>     WHERE (K.LastName = K2.LastName) AND
>       (K.FirstName = K2.FirstName)
>     )

> Now, combined the two SELECT queries act criteria to determine whether
> a record is deleted or not. To be included in this final result set a
> record must meet BOTH criteria. It must be in a group of two or more
> records AND its DateIn field must not contain the highest date for
> that group.

>   SELECT *
>   FROM "KillDupes.db" K
>   /* Criteria #1 - grouping */
>   WHERE EXISTS
>     (
>     SELECT K1.LastName, K1.FirstName, COUNT(K1.DateIn)
>     FROM "KillDupes.db" K1
>     WHERE (K.LastName = K1.LastName) AND
>       (K.FirstName = K1.FirstName)
>     GROUP BY K1.LastName, K1.FirstName
>     HAVING COUNT(K1.DateIn) > 1
>     )
>   /* Criteria #2 - date field */
>   AND NOT K.DateIn = ANY
>     (
>     SELECT MAX(K2.DateIn)
>     FROM "KillDupes.db" K2
>     WHERE ((K.LastName = K2.LastName) AND
>       (K.FirstName = K2.FirstName))
>     )

> ///////////////////////////////////////////////////////////
> Steve Koterski              "No matter how cynical you get,
> Felton, CA                  it is impossible to keep up."
>                                              -- Lily Tomlin

Other Threads