473,388 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

SELECT duplicates in a table

I've look for a solution to this, but have only been able to find
solutions to delete duplicate entries in a table by deleting entries not
returned by SELECT DISTINCT.

What sql should I use to SELECT entries in a table that have two
particular column values that match?

For example, my_table has
name, phone number, identification_number, zip code, date of birth, and city

I want to SELECT rows from this table that have the same values in
identification and date of birth (duplicates) so I can have the user
look at them in order to figure out which one to delete.

I tried something like:

$db_sql = "SELECT * FROM my_table GROUP BY identification_number
HAVING count(date_of_birth) > 1 ORDER BY name"

but that doesn't seem to work.

Thanks,
Bruce
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
3 42139
Try

SELECT *
FROM mytable
WHERE (identification_number,date_of_birth) IN
(SELECT identification_number
, date_of_birth
FROM mytable m2
GROUP BY identification_number,data_of_birth
HAVING COUNT(*) > 1
)

There are other ways of doing it, perhaps more efficient.

Vincent
I've look for a solution to this, but have only been able to find
solutions to delete duplicate entries in a table by deleting entries not
returned by SELECT DISTINCT.

What sql should I use to SELECT entries in a table that have two
particular column values that match?

For example, my_table has
name, phone number, identification_number, zip code, date of birth, and
city

I want to SELECT rows from this table that have the same values in
identification and date of birth (duplicates) so I can have the user
look at them in order to figure out which one to delete.

I tried something like:

$db_sql = "SELECT * FROM my_table GROUP BY identification_number
HAVING count(date_of_birth) > 1 ORDER BY name"

but that doesn't seem to work.

Thanks,
Bruce
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Thanks. Worked like a charm!

Bruce
vh*****@inreach.com wrote:
Try

SELECT *
FROM mytable
WHERE (identification_number,date_of_birth) IN
(SELECT identification_number
, date_of_birth
FROM mytable m2
GROUP BY identification_number,data_of_birth
HAVING COUNT(*) > 1
)

There are other ways of doing it, perhaps more efficient.

Vincent

I've look for a solution to this, but have only been able to find
solutions to delete duplicate entries in a table by deleting entries not
returned by SELECT DISTINCT.

What sql should I use to SELECT entries in a table that have two
particular column values that match?

For example, my_table has
name, phone number, identification_number, zip code, date of birth, and
city

I want to SELECT rows from this table that have the same values in
identification and date of birth (duplicates) so I can have the user
look at them in order to figure out which one to delete.

I tried something like:

$db_sql = "SELECT * FROM my_table GROUP BY identification_number
HAVING count(date_of_birth) > 1 ORDER BY name"

but that doesn't seem to work.

Thanks,
Bruce
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
Assuming identification_number is a unique (primary) key...

select * from my_table where date_of_birth in (select date_of_birth
from my_table group by date_of_birth having count(*) > 1)

Or - it may be quicker to do...

select * from my_table a where exists (select 'x' from my_table b where
a.date_of_birth = b.date_of_birth group by b.date_of_birth having
count(*) > 1)
Kall, Bruce A. wrote:
I've look for a solution to this, but have only been able to find
solutions to delete duplicate entries in a table by deleting entries
not returned by SELECT DISTINCT.

What sql should I use to SELECT entries in a table that have two
particular column values that match?

For example, my_table has
name, phone number, identification_number, zip code, date of birth,
and city

I want to SELECT rows from this table that have the same values in
identification and date of birth (duplicates) so I can have the user
look at them in order to figure out which one to delete.

I tried something like:

$db_sql = "SELECT * FROM my_table GROUP BY identification_number
HAVING count(date_of_birth) > 1 ORDER BY name"

but that doesn't seem to work.

Thanks,
Bruce
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John | last post by:
Why does a select from table with an integer field return a row if I compare the integer to an alpha character? Here's the info ( a straight copy and paste), then compare my two select...
2
by: hubert.trzewik | last post by:
Hello, Is it possible to EXEC stored procedure from a query? I want to execute stored procedure for every line of SELECT result table. I guess it's possible with cursors, but maybe it's...
13
by: Botao | last post by:
Hi, Every Guru, I'd like to put a button on a page. When clicking the button, the table below it gets selected so the user can do Ctrl C to copy the entire table without using the mouse to...
9
by: saturnius | last post by:
Hello, I am passing a value to a stored procedure in vb.net to get normally a selection of the table. How could I get the complete table without writing the code twice? Many thanks in advance ....
6
by: Eugene F | last post by:
I am trying to figure out a single select statement from a table function f(x) when the function's argument is fed from another result set, like SELECT ... FROM TABLE( f(x) ) a where x is a...
2
by: pagoto123 | last post by:
select * from table where director = '" & Director.text & "' i want to display me all results that start with the director that the user will enter........ please i need the command help me...
4
by: d0m_at0m | last post by:
hello, how to select a table that has a highest amount of columns (compared to other tables) using syscat? thanks in advance
9
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all On SQL Server tables I use SqlDataReader to query data like "SELECT * FROM table". Is there something similar for XML Files ? I want to use SQL Syntax like "SELECT * FROM table" on a...
0
by: onyris | last post by:
Hi have this query which works ok now : SELECT * FROM WHERE (((Table.published) Like '*'&!!& '*' And (Table.published) Like '*'&!!& '*' and (Table.address) Like '*'&!!& '*' )); and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.