pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedRe[2]: [SQL] Query to eliminate duplicates
3+ messages / 3 participants
[nested] [flat]
* Re[2]: [SQL] Query to eliminate duplicates
@ 1998-12-03 14:47 Sferacarta Software <sferac@bo.nettuno.it>
1998-12-03 15:07 ` RE: Re[2]: [SQL] Query to eliminate duplicates Mario Filipe <mjnf@uevora.pt>
0 siblings, 1 reply; 3+ messages in thread
From: Sferacarta Software @ 1998-12-03 14:47 UTC (permalink / raw)
To: Mario Filipe <mjnf@uevora.pt>; pgsql-sql
Hello Mario,
giovedì, 3 dicembre 98, you wrote:
MF> On 02-Dec-98 Sferacarta Software wrote:
>> MF> Is there a way to eliminate duplicate records using just SQL?
>>
>> SELECT DISTINCT ...
MF> I deserved that!
MF> The problem is that i have a table where there are duplicate records
MF> and i want to delete them! I tryed creating a unique index but it told me it
MF> couldn't because it didn't have a function to do something (I think it is
MF> because i had a boolean field in it)
select * from TABELA where COLUNA in (
select COLUNA from TABELA group by COLUNA having 1 < count(COLUNA)
);
This query should retrieve all rows where COLUNA field contains
duplicate values.
Unfortunately, seems it doesn't work on v6.4, but you may
do: select COLUNA from TABELA group by COLUNA having 1 < count(COLUNA)
to display only COLUNA field.
EXAMPLE:
select * from emp;
ename |empno|job | hiredate|sal |comm|deptno|level| mgr
------+-----+----------+----------+---------+----+------+-----+----
ALLEN | 7499|SALESMAN |1981-02-20|$1,600.00| 300| 20| 4|7782
BLAKE | 7698|MANAGER |1981-05-01|$2,850.00| | 30| 3|7782
JONES | 7900|CLERK |1981-12-03|$950.00 | | 30| 2|7782
MILLER| 7654|SALESMAN |1981-09-28|$1,250.00| 400| 30| 3|7839
CLARK | 7844|SALESMAN |1981-09-08|$1,500.00| | 10| 2|7839
KING | 7521|SALESMAN |1981-02-22|$1,250.00| 500| 10| 1|7782
ALLEN | 7499|SALESMAN |1981-02-20|$1,600.00| 300| 20| 4|7782
BLAKE | 7698|MANAGER |1981-05-01|$2,850.00| | 30| 3|7782
(8 rows)
select ename from emp group by ename having 1 < count(ename);
ename
-----
ALLEN
BLAKE
(2 rows)
-Jose'-
^ permalink raw reply [nested|flat] 3+ messages in thread
* RE: Re[2]: [SQL] Query to eliminate duplicates
1998-12-03 14:47 Re[2]: [SQL] Query to eliminate duplicates Sferacarta Software <sferac@bo.nettuno.it>
@ 1998-12-03 15:07 ` Mario Filipe <mjnf@uevora.pt>
1998-12-03 20:02 ` Re: Re[2]: [SQL] Query to eliminate duplicates Leslie Mikesell <les@Mcs.Net>
0 siblings, 1 reply; 3+ messages in thread
From: Mario Filipe @ 1998-12-03 15:07 UTC (permalink / raw)
To: Sferacarta Software <sferac@bo.nettuno.it>; +Cc: pgsql-sql
On 03-Dec-98 Sferacarta Software wrote:
> Hello Mario,
>
> giovedì, 3 dicembre 98, you wrote:
>
>
> MF> On 02-Dec-98 Sferacarta Software wrote:
>>> MF> Is there a way to eliminate duplicate records using just SQL?
>>>
>>> SELECT DISTINCT ...
>
> MF> I deserved that!
>
> MF> The problem is that i have a table where there are duplicate
> records
> MF> and i want to delete them! I tryed creating a unique index but it told me
> it
> MF> couldn't because it didn't have a function to do something (I think it is
> MF> because i had a boolean field in it)
>
>
> select * from TABELA where COLUNA in (
> select COLUNA from TABELA group by COLUNA having 1 < count(COLUNA)
> );
>
This would work (i believe you) but in my case i'm looking into a table
where there are at least 3 columns to look at!
It's a table where I keep the information about wich are the
disciplines where a student is enrolled:
Ex:
numero_aluno|codigo_disciplina|ano_lectivo|epoca|semestre
------------+-----------------+-----------+-----+--------
10335| 1207|1998/1999 | 1|f
10335| 1208|1998/1999 | 1|f
10335| 1209|1998/1999 | 1|f
10335| 1203|1998/1999 | 1|f
10335| 1205|1998/1999 | 1|f
10335| 1212|1998/1999 | 1|f
10335| 1213|1998/1999 | 1|f
10335| 1215|1998/1999 | 1|f
In this case there are no duplicates but in this one there are:
numero_aluno|codigo_disciplina|ano_lectivo|epoca|semestre
------------+-----------------+-----------+-----+--------
13427| 35|1998/1999 | 1|t
13427| 904|1998/1999 | 1|f
13427| 122|1998/1999 | 1|t
13427| 907|1998/1999 | 1|f
13427| 481|1998/1999 | 1|f
13427| 286|1998/1999 | 1|t
13427| 368|1998/1999 | 1|t
13427| 35|1998/1999 | 1|t
13427| 904|1998/1999 | 1|f
13427| 122|1998/1999 | 1|t
So a duplicate is actually a record that looks exactly like other record on the
table...
Thanks for your help anyway
Mario Filipe
mjnf@uevora.pt
http://neptuno.sc.uevora.pt/~mjnf
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Re[2]: [SQL] Query to eliminate duplicates
1998-12-03 14:47 Re[2]: [SQL] Query to eliminate duplicates Sferacarta Software <sferac@bo.nettuno.it>
1998-12-03 15:07 ` RE: Re[2]: [SQL] Query to eliminate duplicates Mario Filipe <mjnf@uevora.pt>
@ 1998-12-03 20:02 ` Leslie Mikesell <les@Mcs.Net>
0 siblings, 0 replies; 3+ messages in thread
From: Leslie Mikesell @ 1998-12-03 20:02 UTC (permalink / raw)
To: mjnf@uevora.pt; +Cc: sferac@bo.nettuno.it; pgsql-sql
According to Mario Filipe:
>
> >>> MF> Is there a way to eliminate duplicate records using just SQL?
> >>>
> >>> SELECT DISTINCT ...
> >
> >
> > MF> The problem is that i have a table where there are duplicate
> > records
> > MF> and i want to delete them! I tryed creating a unique index but it told me
> > it
> > MF> couldn't because it didn't have a function to do something (I think it is
> > MF> because i had a boolean field in it)
>
> So a duplicate is actually a record that looks exactly like other record on the
> table...
How about:
select distinct * into newtable from oldtable ;
drop table oldtable;
alter table newtable rename to oldtable;
You have to drop/create indexes separately, even the ones built
implicitly from 'primary key' directives.
Les Mikesell
les@mcs.com
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~1998-12-03 20:02 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-12-03 14:47 Re[2]: [SQL] Query to eliminate duplicates Sferacarta Software <sferac@bo.nettuno.it>
1998-12-03 15:07 ` Mario Filipe <mjnf@uevora.pt>
1998-12-03 20:02 ` Leslie Mikesell <les@Mcs.Net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox