Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Removing double records, math problem
Message
De
16/06/2007 16:12:48
 
 
À
16/06/2007 04:50:01
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01233421
Message ID:
01233748
Vues:
9
I Think this should do it for you.
**** PseudoCode, untested, not optimized
**** Assumes Each table has identical structure.
**** Assumes Each Table has a field called Key used to test for Duplication.
**** Assumes Each Table has a field called Source that is the same value
****   For each record in that file and is unique to that file.

USE Table1
COPY STRUCTURE TO Output
SELECT * FROM Table1 INTO CURSOR InpMaster READWRITE 
APPEND FROM Table2
APPEND FROM Table3
APPEND FROM Table4

SELECT InpMaster.*, Dups.Cnt AS DupCnt;
  FROM InpMaster LEFT OUTER JOIN ;
    (SELECT Key, COUNT(*) FROM InpMaster GROUP BY 1) AS Dups ON InpMaster.Key = Dups.Key;
    INTO CURSOR Master
USE IN InpMaster
    
SELECT Master.Source, 000000 as Cnt, COUNT(*) as TotDups;
  FROM Master GROUP BY 1 WHERE Master.DupCnt > 1 INTO CURSOR DupCnt READWRITE 
  
USE Output IN 0
SELECT Master

SCAN
  SCATTER NAME oRecData
  cKey    = Master.Key
  cSource = Master.Source
  SELECT Output
  LOCATE FOR cKey = Output.Key && IRL use seek and an index
  IF FOUND()
    cDupSource = Output.SourceCode 
    IF cDupSource != cSource && Should Not be Equal based on given 
                             && scenario but happens 
                             && in real life so trap it anyway.
      SELECT DupCnt
      LOCATE FOR DupCnt.SourceCode = cSource         && This Tables Stats
      nThisPerc = DupCnt.Cnt / DupCnt.TotDups        && Get Percentage
      LOCATE FOR DupCnt.SourceCode = cDupSource      && Other Tables Stats
      nOtherPerc = DupCnt.Cnt / DupCnt.TotDups       && Get Percentage
      IF nOtherPerc > nThisPerc  
        **** Exchange other record with this one
        SELECT Output
        GATHER NAME oRecData
        *** Update Stats
        SELECT DupCnt
        REPLACE DupCnt.Count WITH DupCnt.Count - 1
        LOCATE FOR DupCnt.SourceCode = cSource
        REPLACE DupCnt.Count WITH DupCnt.Count + 1
      ENDIF
    ENDIF
  ELSE
    *** Not a Duplicate yet add to output
    INSERT INTO Output FROM NAME oRecData
    REPLACE Output.SourceCode WITH cSource
    IF Master.Dupcnt > 1
      SELECT DupCnt
      LOCATE FOR DupCnt.SourceCode = cSource         && This Tables Stats
      REPLACE DupCnt.Count WITH DupCnt.Count + 1
    ENDIF
  ENDIF
  SELECT Master
ENDSCAN
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform