Question

Delete the records that have the minimum of records, I have 2 tables person (id, otherID) otherID is a reference of an other person.id and film (id, country, personID) where personID is a foreign key of person.id

I want to delete all the records in film table that have the minimum of records relative to personID and country. For example:

Person(1, 2)
Person(2, 2)
Person(3, 2)
Film(1, fr, 1)
Film(2, uk, 1)
Film(3, fr, 2)
Film(4, fr, 3)
Film(5, usa, 1)
Film(6, fr, 1)

I have to remove Film(3, fr, 2) Person(1, 2 )->because count of person 1 in the country fr, 2, is upper then the count of other person 2 (otherID column) in the same country 'fr',1.

Person(2, 2) nothing to remove

Person(3, 2) delete one of the records Film(3, fr, 2) or Film(4, fr, 3) because both have fr as country and count = 1 but as the record Film(3, fr, 2) is deleted before so Film(4, fr, 3) have to be kept.

where count = select count(*) over (partition by film.personID, film.country) from film

and keep

Film(1, fr, 1)
Film(2, uk, 1)
Film(4, fr, 3)
Film(5, usa, 1)
Film(6, fr, 1)

Actually for each record in person table we ll look for:

a = select count(*) over (partition person.id,film.country)

b = select count(*) over (partition person.otherID,film.country)

The same film.country for both a and b and then delete the record which have min(a,b)

Was it helpful?

Solution

If you can write a query that either identifies the rows that are to be deleted or identifies the rows that are not to be deleted, and which ideally returns a set of ROWID's, then you can simply:

Delete from my table where rowid in ( ...);

or

Delete from my table where rowid not in ( ...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top