【问题标题】:Delete a query with analytic function使用分析函数删除查询
【发布时间】:2012-05-24 19:48:26
【问题描述】:

删除记录最少的记录,我有 2 个表 person (id, otherID) otherID 是其他 person.id 和电影 (id, country, personID) 的引用,其中 personID 是 person 的外键.id

我想删除电影表中与 personID 和 country 相关的记录最少的所有记录。例如:

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)

我必须删除 电影(3, fr, 2) Person(1, 2 )->因为国家 fr, 2 中的人 1 的计数高于同一国家 'fr',1 中的其他人 2(otherID 列)的计数。

Person(2, 2) 没有什么可删除的

Person(3, 2) 删除 Film(3, fr, 2) 或 Film(4, fr, 3) 记录之一,因为两者都有 fr 作为国家并且 count = 1 但作为记录 Film(3, fr , 2) 之前被删除,因此必须保留 Film(4, fr, 3)。

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

并保持

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

实际上,对于 person 表中的每条记录,我们都会查找:

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

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

a和b同一个film.country,然后删除min(a,b)的记录

【问题讨论】:

  • 您能花点时间写下您的问题吗?在尝试解决您的查询时,它至少应包含对what you have tried明确描述。
  • @R Vive Lol:同意 Ben 的观点,我不明白你是怎么计算的?请说清楚

标签: oracle oracle10g sql-delete analytic-functions


【解决方案1】:

如果您可以编写一个查询来标识要删除的行标识不被删除的行,并且理想情况下返回一组 ROWID,那么您可以简单地:

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

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

【讨论】:

    猜你喜欢
    • 2013-03-06
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多