Thread: Removing duplicate rows

Removing duplicate rows

From
Paul Lambert
Date:
I have some tables which I need to load from flat data files that can 
contain upwards of five million rows.

It's possible (owing to the structure of the system the data is 
originating from) that on occasion the data extract that generates these 
flat files can include two records that have the same values in the 
fields that make up the primary key of the table in my Postgres 
database, thus throwing an error when I attempt to load.

I'm assuming the best way to get around this is to load the data into a 
temporary table with "copy from" and then do a "select distinct into" my 
real table.

The question I have is the best way to set up this temporary table in my  reload script. (Having 14 servers running
Postgres,each with 37 
 
tables, I don't want to be creating temporary tables manually each time 
I need to reload the databases)

As the table definitions get updated frequently, I'd like to put in my 
script to create the table as a copy of the existing 'real' table. I'm 
assuming "create table as" would be what I need to use, but what would I 
use as the select sql - i.e. how do I use select to pull the definition 
of a table.

Apologies if this is a stupid question, I'm still fairly new to Postgres 
so I'm not sure what system tables are available for pulling out this 
kind of information.

Thanks in advance for any assistance you can offer.

Regards,
Paul.

-- 
Paul Lambert
Database Administrator
AutoLedgers



Re: Removing duplicate rows

From
Andrew Sullivan
Date:
On Wed, Feb 07, 2007 at 07:15:02AM +0900, Paul Lambert wrote:
> 
> I'm assuming the best way to get around this is to load the data into a 
> temporary table with "copy from" and then do a "select distinct into" my 
> real table.

You might find that sort|uniq at the command prompt would be better. 
That said,

> script to create the table as a copy of the existing 'real' table. I'm 
> assuming "create table as" would be what I need to use, but what would I 
> use as the select sql - i.e. how do I use select to pull the definition 
> of a table.

an easy way to do this is "CREATE TABLE name AS SELECT . . . WHERE
1=0".  You get a table with no rows.  (WHERE FALSE and similar
constructs all work equally well.)

A

-- 
Andrew Sullivan  | ajs@crankycanuck.ca
In the future this spectacle of the middle classes shocking the avant-
garde will probably become the textbook definition of Postmodernism.                --Brad Holland


Re: Removing duplicate rows

From
Paul Lambert
Date:
Andrew Sullivan wrote:
> On Wed, Feb 07, 2007 at 07:15:02AM +0900, Paul Lambert wrote:
>> I'm assuming the best way to get around this is to load the data into a 
>> temporary table with "copy from" and then do a "select distinct into" my 
>> real table.
> 
> You might find that sort|uniq at the command prompt would be better. 
> That said,

Sort on Weenblows is a bastard to work with, and I don't believe it has 
a unique option. I probably should have mentioned this was on Weenblows.    

> 
>> script to create the table as a copy of the existing 'real' table. I'm 
>> assuming "create table as" would be what I need to use, but what would I 
>> use as the select sql - i.e. how do I use select to pull the definition 
>> of a table.
> 
> an easy way to do this is "CREATE TABLE name AS SELECT . . . WHERE
> 1=0".  You get a table with no rows.  (WHERE FALSE and similar
> constructs all work equally well.)

Thanks... I figured it would be blatantly obvious.

This does the trick:
CREATE TABLE billing_code_temp AS SELECT * FROM billing_code WHERE 1=0

> 
> A
> 

Appreciate the help.

P.

-- 
Paul Lambert
Database Administrator
AutoLedgers



Re: Removing duplicate rows

From
Hélder M. Vieira
Date:
> As the table definitions get updated frequently, I'd like to put in my 
> script to create the table as a copy of the existing 'real' table. I'm 
> assuming "create table as" would be what I need to use, but what would I 
> use as the select sql - i.e. how do I use select to pull the definition of 
> a table.


The create statement allows you to copy a table structure, including or 
excluding constraints.
For inclusion and exclusion effects, please read the manual info related 
with the 'like' clause of the 'create table' statement.

Ex.:
create temporary table mytesttable (like mymaintable excluding constraints 
excluding defaults);


Regards,

Hélder M. Vieira 



Re: Removing duplicate rows

From
Thomas Kellerer
Date:
Paul Lambert wrote on 06.02.2007 23:44:
> Sort on Weenblows is a bastard to work with, and I don't believe it has 
> a unique option. I probably should have mentioned this was on Weenblows.   

You can get all (or most?) of the *nix/GNU commandline tools for Windows as 
well. As "native" Win32 programs that do not require a pseudo *nix (aka as 
Cygwin) to run in:

http://gnuwin32.sourceforge.net

tsort and uniq are part of the coreutils package

Thomas