I've got a table with 6 columns:
id
name
type_id
code
lat
long
The first three are required. ID
is the private key, inserted automatically with a sequence.
I have some rows that are duplicates, as defined by BOTH the name
and type_id
being equal, but i'd like to view all the data for the dupes. I can find the dupes simply enough:
SELECT name
, type_id
FROM table1
GROUP BY name
, type_id
HAVING COUNT(*) > 1
but actually viewing all the info is confounding me. I know this should be simple, but I'm hitting a wall here.
You can always use the GROUP BY
/ HAVING
query in an IN clause. This works and is relatively straightforward but it may not be particularly efficient if the number of duplicate rows is relatively large.
SELECT *
FROM table1
WHERE (name, type_id) IN (SELECT name, type_id
FROM table1
GROUP BY name, type_id
HAVING COUNT(*) > 1)
It would generally be more efficient to use analytic functions in order to avoid hitting the table a second time.
SELECT *
FROM (SELECT id,
name,
type_id,
code,
lat,
long,
count(*) over (partition by name, type_id) cnt
FROM table1)
WHERE cnt > 1
Depending on what you are planning to do with the data and how many duplicates of a particular row there might be, you also might want to join table1
to itself to get the data in a single row
SELECT a.name,
a.type_id,
a.id,
b.id,
a.code,
b.code,
a.lat,
b.lat,
a.long,
b.long
FROM table1 a
JOIN table1 b ON (a.name = b.name AND
a.type_id = b.type_id AND
a.rowid > b.rowid)
SELECT *
FROM table1 t1
WHERE (t1.name,t1.type_id) in ( SELECT DISTINCT name
, type_id
FROM table1
GROUP BY name, type_id
HAVING COUNT(*) > 1 )
Would do it.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With