Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove duplicated records

Posted on 2002-04-14
8
Medium Priority
?
139 Views
Last Modified: 2010-04-04
In my table I have a big number of duplicated records.
How can I remove these so I have just unique records ?
A simple project that do this job ?

Regards,
Nick

P.S. I use a dbf table .
0
Comment
Question by:ginsonic
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6939781
use a query like
select distinct * from tablename

create a a temporary table with the resultset(batchmove)
empty the original table

insert the temporary table entries into the original-table (batchmove)

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 6939798
Can you send me a sample project ? I'm just an amateur programmer and is the first time when play with a database ( at this stage ).
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6939875
Nick, meikll :-)

there is a nested SQL statement that allow to delete "duplicate" records from the table. "Duplicate" - mean that every record has unique primary key but rest of fields are the same.

delete from sometable where id_field not in
(
select min(id_field) from sometable
group by field1, field2, field3
)

Nick, is it your case?

-----
Igor
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6939886
igor,
if there are an id-field,
then i don't have duplicate entries,
but if it depends on a part of fields,
then it would work.

nick, sample is in work

meikl ;-)
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6939891
yes meikl, this is why  I wrote duplicated as "duplicated".
0
 
LVL 9

Author Comment

by:ginsonic
ID: 6939910
Wait then at ginsonic@xnet.ro :)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 400 total points
ID: 6939965
sample ready,
will not work if there are constraints
defined on the table
(master-detail-relationships)

unit db_del_duplicates_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBTables, Db, StdCtrls, bde;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    Button1: TButton;
    procedure ComboBox1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


//load aliases
procedure TForm1.FormCreate(Sender: TObject);
begin
  session.GetAliasNames(combobox1.Items);
end;

//load tables
procedure TForm1.ComboBox1Change(Sender: TObject);
var
  DriverName : String;
begin
  combobox2.text := ''; //clear
  DriverName := session.GetAliasDriverName(combobox1.text);
  if (DriverName = 'STANDARD') then
    session.GetTableNames(combobox1.text,'*.db?',true,false,combobox2.items)
  else
    session.GetTableNames(combobox1.text,'',false,false,combobox2.items);
end;

procedure do_delete_duplicates(aliasname, tablename : string);
const
  sqltext = 'select distinct * from %s';
var
  origtable, temptable : ttable;
  q : tquery;
begin
  q := tquery.create(nil);
  origtable := ttable.create(nil);
  temptable := ttable.create(nil);
  try
    //supply query
    q.DatabaseName := aliasname;
    q.SQL.text := format(sqltext,[tablename]);

    //supply temptable
    temptable.DatabaseName := aliasname;
    temptable.tablename := 'temptable';

    //suplly origtable
    origtable.DatabaseName := aliasname;
    origtable.tablename := tablename;

    //extract unique
    q.open;

    //save into temtable
    temptable.BatchMove(q,batcopy);

    //query not needed now
    q.close;

    //empty originaltable
    origtable.EmptyTable;

    //recopy unique-entries
    origtable.BatchMove(temptable,batAppend);

    //delete temptable
    temptable.DeleteTable;

  finally
    q.free;
    origtable.free;
    temptable.free;
  end;
end;

//sample usage
procedure TForm1.Button1Click(Sender: TObject);
begin
  do_delete_duplicates(combobox1.Text,combobox2.text);
end;

end.


i can send you source and app,
if further needed

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 6940030
Done . I have now with ~ 1000 records less , thanks .
0

Featured Post

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!

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

722 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