Solved

Remove duplicated records

Posted on 2002-04-14
8
133 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
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 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 100 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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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