Solved

Remove duplicated records

Posted on 2002-04-14
8
128 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now