dbTalk Databases Forums  

eliminate duplicates

comp.databases.postgresql comp.databases.postgresql


Discuss eliminate duplicates in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael
 
Posts: n/a

Default eliminate duplicates - 01-14-2009 , 11:00 AM






Hi,

result of my query has duplicates on one column like that:

id attr name fname
3 def Gerry gerry
3 en gerry gerry
1 ac fred fred


how do I eliminate a row with id 3 so that there is only one on result

i 've tried group by id, but then i get error that other fields must
apper in group clause.

Best regards, Michael

Reply With Quote
  #2  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: eliminate duplicates - 01-14-2009 , 12:41 PM






Michael <useitall (AT) gmail (DOT) com> wrote:
Quote:
Hi,

result of my query has duplicates on one column like that:

id attr name fname
3 def Gerry gerry
3 en gerry gerry
1 ac fred fred


how do I eliminate a row with id 3 so that there is only one on result
for your example:

test=# SELECT * from michael ;
id | attr | name | fname
----+------+-------+-------
3 | def | Gerry | gerry
3 | en | gerry | gerry
1 | ac | fred | fred
(3 Zeilen)

Zeit: 0,184 ms
test=*# delete from michael where ctid not in (select distinct on (id) ctid from michael);
DELETE 1
Zeit: 0,423 ms
test=*# SELECT * from michael ;
id | attr | name | fname
----+------+-------+-------
3 | def | Gerry | gerry
1 | ac | fred | fred
(2 Zeilen)


For the future: use a primary key ...


HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°


Reply With Quote
  #3  
Old   
Michael
 
Posts: n/a

Default Re: eliminate duplicates - 01-14-2009 , 02:03 PM



On Jan 14, 7:41*pm, Andreas Kretschmer <akretsch... (AT) spamfence (DOT) net>
wrote:
Quote:
Michael <useit... (AT) gmail (DOT) com> wrote:
Hi,

result of my query has duplicates on one *column like that:

id *attr * * * *name * fname
3 * def * * * * Gerry *gerry
3 * en * * * * *gerry *gerry
1 * ac * * * * *fred * fred

how do I eliminate a row with id 3 so that there is only one on result

for your example:

test=# SELECT * from michael ;
*id | attr | name *| fname
----+------+-------+-------
* 3 | def *| Gerry | gerry
* 3 | en * | gerry | gerry
* 1 | ac * | fred *| fred
(3 Zeilen)

Zeit: 0,184 ms
test=*# delete from michael where ctid not in (select distinct on (id) ctid from michael);
DELETE 1
Zeit: 0,423 ms
test=*# SELECT * from michael ;
*id | attr | name *| fname
----+------+-------+-------
* 3 | def *| Gerry | gerry
* 1 | ac * | fred *| fred
(2 Zeilen)

For the future: use a primary key ...

HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. * * * * * * * * * * * * * * *(Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." * (unknown)
Kaufbach, Saxony, Germany, Europe. * * * * * * *N 51.05082°, E 13.56889°
Hi Andreas,
thanks for your suggestions.
I don't wont to delete data from table,because i need them all, only
output of query should not have any duplicates.

SELECT * FROM table where fname = 'gerry' and (attr = 'en' or attr =
'def') /* there are also other gerry in table with id3 but diffrent
attr */

Regards, Michael



Reply With Quote
  #4  
Old   
Nico Latzer
 
Posts: n/a

Default Re: eliminate duplicates - 01-14-2009 , 04:07 PM



Hi Michael, your last reply indicates that Andreas is very right on
strengthen the need for a primary key (over id, attr), otherwise you
will always fight with different "versions" of Your records, which one
to exclude in Your view? etc.
Kind Regards, Nico

Michael schrieb:
Quote:
On Jan 14, 7:41 pm, Andreas Kretschmer <akretsch... (AT) spamfence (DOT) net
wrote:
Michael <useit... (AT) gmail (DOT) com> wrote:
Hi,
result of my query has duplicates on one column like that:
id attr name fname
3 def Gerry gerry
3 en gerry gerry
1 ac fred fred
how do I eliminate a row with id 3 so that there is only one on result
for your example:

test=# SELECT * from michael ;
id | attr | name | fname
----+------+-------+-------
3 | def | Gerry | gerry
3 | en | gerry | gerry
1 | ac | fred | fred
(3 Zeilen)

Zeit: 0,184 ms
test=*# delete from michael where ctid not in (select distinct on (id) ctid from michael);
DELETE 1
Zeit: 0,423 ms
test=*# SELECT * from michael ;
id | attr | name | fname
----+------+-------+-------
3 | def | Gerry | gerry
1 | ac | fred | fred
(2 Zeilen)

For the future: use a primary key ...

HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

Hi Andreas,
thanks for your suggestions.
I don't wont to delete data from table,because i need them all, only
output of query should not have any duplicates.

SELECT * FROM table where fname = 'gerry' and (attr = 'en' or attr =
'def') /* there are also other gerry in table with id3 but diffrent
attr */

Regards, Michael


Reply With Quote
  #5  
Old   
Michael
 
Posts: n/a

Default Re: eliminate duplicates - 01-15-2009 , 03:11 AM



On Jan 14, 11:07*pm, Nico Latzer <n... (AT) mnet-online (DOT) de> wrote:
Quote:
Hi Michael, your last reply indicates that Andreas is very right on
strengthen the need for a primary key (over id, attr), otherwise you
will always fight with different "versions" of Your records, which one
to exclude in Your view? etc.
Kind Regards, Nico

Michael schrieb:

On Jan 14, 7:41 pm, Andreas Kretschmer <akretsch... (AT) spamfence (DOT) net
wrote:
Michael <useit... (AT) gmail (DOT) com> wrote:
Hi,
result of my query has duplicates on one *column like that:
id *attr * * * *name * fname
3 * def * * * * Gerry *gerry
3 * en * * * * *gerry *gerry
1 * ac * * * * *fred * fred
how do I eliminate a row with id 3 so that there is only one on result
for your example:

test=# SELECT * from michael ;
*id | attr | name *| fname
----+------+-------+-------
* 3 | def *| Gerry | gerry
* 3 | en * | gerry | gerry
* 1 | ac * | fred *| fred
(3 Zeilen)

Zeit: 0,184 ms
test=*# delete from michael where ctid not in (select distinct on (id) ctid from michael);
DELETE 1
Zeit: 0,423 ms
test=*# SELECT * from michael ;
*id | attr | name *| fname
----+------+-------+-------
* 3 | def *| Gerry | gerry
* 1 | ac * | fred *| fred
(2 Zeilen)

For the future: use a primary key ...

HTH, Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. * * * * * * * * * * ** * * *(Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." * (unknown)
Kaufbach, Saxony, Germany, Europe. * * * * * * *N 51.05082°, E 13.56889°

Hi Andreas,
thanks for your suggestions.
I don't wont to delete data from table,because i need them all, only
output of query should not have any duplicates.

SELECT * FROM table where fname = 'gerry' and (attr = 'en' or attr =
'def') /* there are also other gerry in table with id3 but diffrent
attr */

Regards, Michael
what is, if primary key is (id,attr)

Regards, Michael


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.