我需要清理一个填充了重复值的数据库,我想删除所有重复的旧日期。目的:
- 如果序列ID有> 1个条目,删除除最新日期之外的所有日期
Asset_Table (显示所有重复的Serial_ID)
Serial_ID | ISSI | Date_Added
---------------------------------------------------------
2PND00849EWS8J1 | 3766040 | 2012-07-06 08:52:23.000
2PND00849EWS8J1 | 3778051 | 2016-09-26 09:21:57.000
预期结果:
Serial_ID | ISSI | Date_Added
---------------------------------------------------------
2PND00849EWS8J1 | 3778051 | 2016-09-26 09:21:57.000
答案 0 :(得分:4)
使用@model IEnumerable<OnlineTaxiReservationSystem.Models.Comment>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserComments)
</th>
@*<th>
@Html.DisplayNameFor(model => model.UserId)
</th>*@
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserComments)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>*@
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.CommentId }) |
@Html.ActionLink("Details", "Details", new { id=item.CommentId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.CommentId })
</td>
</tr>
}
</table>
功能避免重复或删除
row_number()
答案 1 :(得分:1)
select
a.Serial_ID ,
a.ISSI ,
Date_Added
from Asset_table as a
where a.Date_Added =
(select max(b.Date_Added)
from Asset_table as b
where b.serial = a.serial)