如何删除每组较小的记录?
我的表 price 具有当前架构:
item date shift price1 price2
1 20110723 day 40 50
1 20110723 night 42 52
1 20110723 weekend 42 52
2 20110723 Night 40 50
...
并且我想保留表中每个项目具有最高价格 1 或价格 2 的记录,即从表中删除具有较小价格 1 或价格 2 的记录。 sql怎么写?
我使用sybase ASE 12.5
My table price has current schema:
item date shift price1 price2
1 20110723 day 40 50
1 20110723 night 42 52
1 20110723 weekend 42 52
2 20110723 Night 40 50
...
And I want to keep the records with highest price1 or price2 for each item in the table, i.e., remove records with either smaller price1 or price2 from the table.
How to write the sql?
I use sybase ASE 12.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 SQL Server(可能还有其他一些 RDBMS)中,您可以使用 CTE和排名,如下所示:
更新
发布整个测试脚本,包括上面的 DELETE 语句,以便任何愿意的人都可以使用它或只是验证它是否有效:
In SQL Server (and probably some other RDBMSes) you can use a CTE and ranking, like this:
UPDATE
Posting the entire testing script, including the above DELETE statement, so anyone willing can play with it or just verify whether it works:
如果你在 oracle 中工作,你可以编写以下查询 -
我听说 sql server 或 mysql 中没有 rowid ...
请告诉我们您正在使用的数据库名称。
你也可以写如下..
you can write following query, if you are working in oracle -
i heard that rowid is not there in sql server or mysql ...
please tell us about your database name which one you are using.
you can write as follow also..