Question

I want to delete exactly 2 rows/records for each employee that is working in more than 3 projects. Let's say I have this table:

+----------+-------------+
| employee |  Project    |
+----------+-------------+
|   1      |   p1        |
|   1      |   p2        |
|   1      |   p3        |
|   1      |   p4        |
|   2      |   p1        |
|   2      |   p3        |
|   3      |   p1        |
|   3      |   p4        |
|   3      |   p5        |
+----------+-------------+

I can query which employees are working in more than 3 projects. In this case the employee with id 1 and the employee with id 3. The query should be:

select employee
  from (
    select employee, count(*) my_count
      from my_table
      group by employee
  ) VW
  where VW.my_count >= 3;

It is not important which rows to delete, what is relevant is to delete two rows/records for every employee that works in more than three projects. The resulting table could be for example:

+----------+-------------+
| employee |  Project    |
+----------+-------------+
|   1      |   p1        |
|   1      |   p2        |
|   2      |   p1        |
|   2      |   p3        |
|   3      |   p1        |
+----------+-------------+
Was it helpful?

Solution 2

SQL> select * from t;

  EMPLOYEE PR                                                                   
---------- --                                                                   
         1 p1                                                                   
         1 p2                                                                   
         1 p3                                                                   
         1 p4                                                                   
         2 p1                                                                   
         2 p3                                                                   
         3 p1                                                                   
         3 p4                                                                   
         3 p5                                                                   

SQL> delete from t
  2  where rowid in (
  3   select rid from (
  4          select rowid rid,
  5          row_number() over(partition by employee
  6          order by project desc) rn,
  7          count(*) over(partition by employee) cnt
  8          from t
  9   ) where cnt >= 3 and rn <=2
 10  )
 11  /

4 rows deleted.

SQL> select * from t;

  EMPLOYEE PR                                                                   
---------- --                                                                   
         1 p1                                                                   
         1 p2                                                                   
         2 p1                                                                   
         2 p3                                                                   
         3 p1        

OTHER TIPS

It is simple approach for this example remove 1000 first rows:

DELETE FROM YOUR_TABLE WHERE ROWID IN 
(SELECT ROWID FROM YOUR_TABLE FETCH FIRST 1000 ROWS ONLY);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top