Domanda

I am looking to create a group indicator for a query using SQL (Oracle specifically). Basically, I am looking for duplicate entries for certain columns and while I can find those what I also want is some kind of indicator to say what rows the duplicates are from.

Below is an example of what I am looking to do (looking for duplicates on Name, Zip, Phone). The rows with Name = aaa are all in the same group, bb are not, and c are.

Is there even a way to do this? I was thinking something with OVER (PARTITION BY ... but I can't think of a way to only increment for each group.

+----------+---------+-----------+------------+-----------+-----------+
| Name     | Zip     | Phone     | Amount     | Duplicate | Group     |
+----------+---------+-----------+------------+-----------+-----------+
| aaa      | 1234    | 5555555   | 500        | X         | 1         |
| aaa      | 1234    | 5555555   | 285        | X         | 1         |
| bb       | 545     | 6666666   | 358        |           | 2         |
| bb       | 686     | 7777777   | 898        |           | 3         |
| aaa      | 1234    | 5555555   | 550        | X         | 1         |
| c        | 5555    | 8888888   | 234        | X         | 4         |
| c        | 5555    | 8888888   | 999        | X         | 4         |
| c        | 5555    | 8888888   | 230        | X         | 4         |
+----------+---------+-----------+------------+-----------+-----------+
È stato utile?

Soluzione

It looks like you can just use

(CASE WHEN COUNT(*) OVER (partition by name, zip, phone) > 1
      THEN 'X'
      ELSE NULL
  END) duplicate,
DENSE_RANK() OVER (ORDER BY name, zip, phone) group_rank

Rows that have the same name, zip, and phone will have the same group_rank. Here is a SQL Fiddle example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top