Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove duplicated records

Posted on 2002-04-14
8
Medium Priority
?
142 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
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 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