如何通过单个查询删除表中字段的重复值?
我想从表中删除日期字段具有多个值的条目。
假设我有员工表 - Id、Name、Date、Points
我想删除具有相同日期字段的条目,该字段应该是唯一的......只是为了清理 我只需要保留一个日期条目并删除其余条目...如果可能的话,也许保留最近的条目...
任何人都可以建议更新查询来执行此操作吗?
I want to delete from a table the entries where it has multiple values for Date field.
So say I have Employee table - Id,Name,Date,Points
I want to delete the entries with same Date field which should be unique...just to cleanup
I need to just keep a single entry for date and delete the rest...maybe keep the recent one if possible....
Can anyone please suggest a update query to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
由于 WHERE 子句和 GROUP BY 不太可能是必需的。仅返回带有聚合函数的一列,但为了安全起见,将其包含在内。这是假设当OP说日期字段时,SQL Server 2008意味着 DATE 数据类型,而不是 DATETIME。
Use:
The GROUP BY isn't likely to be necessary due to the WHERE clause & only returning one column with an aggregate function on it, but it's included to be safe. This is assuming that when the OP says date field, being SQL Server 2008 that means the DATE data type, not DATETIME.
此查询查看具有相同 ID、名称和点的记录,并删除除最新记录之外的所有记录
this query looks at records with same Id,Name and Points and deletes all but the latest
如果你的表有主键,你可以通过 dup 条件将表连接到自身并过滤掉更大的 PK,如下所示:
If your table has primary key, you can join the table to itself by the dup condition and filter out greater PKs, something like the following:
可以使用,或者您可以使用此代码
如果最后一个条目是最近的日期,则
you can use
if the last entry is the recent date, Or you can use this code