SQL 按日期显示每个任务的最新记录
SQL Show most current record for each task by date
我想为每个任务显示最近更新的记录。
目前,无论执行什么任务,我都只会获取所有记录中的最新记录。
这是前端代码
<asp:SqlDataSource ID="GrabAssignedTasks" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT Backlog.Story, Tasks.Task, Tasks.EstimatedHours, Tasks.UserStoryTaskID, Tasks_Log.TaskLogID, Tasks_Log.HoursLeft, Tasks_Log.ModifiedDate
FROM Backlog
INNER JOIN Tasks ON Backlog.UserStoryID = Tasks.UserStoryID
INNER JOIN Tasks_Log ON Tasks.UserStoryTaskID = Tasks_Log.TaskID
WHERE (Tasks.OwnershipID = @UserID) AND (Tasks_Log.ModifiedDate = (SELECT MAX(ModifiedDate) AS Expr1
FROM Tasks_Log AS Tasks_Log_1))">
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="loggedInUser" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<header class="jumbotron hero-spacer">
<asp:Button ID="BackToTasks" runat="server" Text="Back" OnClick="BackToTasks_Click" CssClass="btn btn-primary btn-large" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="GrabAssignedTasks" Height="179px" Width="829px" OnSelectedIndexChanged="HoursChange_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Story" HeaderText="Story" SortExpression="Story" />
<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" />
<asp:BoundField DataField="EstimatedHours" HeaderText="Estimated Hours" SortExpression="EstimatedHours" />
<asp:BoundField DataField="UserStoryTaskID" HeaderText="UserStoryTaskID" SortExpression="UserStoryTaskID" InsertVisible="False" ReadOnly="True" HeaderStyle-CssClass="hidden-field" ItemStyle-CssClass="hidden-field"/>
<asp:BoundField DataField="TaskLogID" HeaderText="TaskLogID" SortExpression="TaskLogID" InsertVisible="False" ReadOnly="True" HeaderStyle-CssClass="hidden-field" ItemStyle-CssClass="hidden-field"/>
<asp:BoundField DataField="HoursLeft" HeaderText="Hours Left" SortExpression="HoursLeft" />
<asp:BoundField DataField="ModifiedDate" HeaderText="Last Updated" SortExpression="ModifiedDate" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
</header>
我在 C# 代码中对此没有任何具体说明。
这是我正在使用的数据库中 table 的图片。
Database table
您没有加入 Table Tasks_Log 和 Tasks_Log_1,所以会发生的是 Tasks_Log 中只有修改日期匹配“30/11/ 2015" 得到返回,不管任务。在 Tasks_Log_1 的 WHERE 子句中添加一个条目,以确保您也在正确的 TaskID 上进行匹配:
SELECT Backlog.Story, Tasks.Task, Tasks.EstimatedHours, Tasks.UserStoryTaskID, Tasks_Log.TaskLogID, Tasks_Log.HoursLeft, Tasks_Log.ModifiedDate
FROM Backlog
INNER JOIN Tasks ON Backlog.UserStoryID = Tasks.UserStoryID
INNER JOIN Tasks_Log ON Tasks.UserStoryTaskID = Tasks_Log.TaskID
WHERE (Tasks.OwnershipID = @UserID)
AND (Tasks_Log.ModifiedDate =
(SELECT MAX(ModifiedDate)
FROM Tasks_Log AS Tasks_Log_1
WHERE Tasks_Log_1.TaskID = Tasks_Log.TaskID))
我想为每个任务显示最近更新的记录。 目前,无论执行什么任务,我都只会获取所有记录中的最新记录。
这是前端代码
<asp:SqlDataSource ID="GrabAssignedTasks" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="SELECT Backlog.Story, Tasks.Task, Tasks.EstimatedHours, Tasks.UserStoryTaskID, Tasks_Log.TaskLogID, Tasks_Log.HoursLeft, Tasks_Log.ModifiedDate
FROM Backlog
INNER JOIN Tasks ON Backlog.UserStoryID = Tasks.UserStoryID
INNER JOIN Tasks_Log ON Tasks.UserStoryTaskID = Tasks_Log.TaskID
WHERE (Tasks.OwnershipID = @UserID) AND (Tasks_Log.ModifiedDate = (SELECT MAX(ModifiedDate) AS Expr1
FROM Tasks_Log AS Tasks_Log_1))">
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="loggedInUser" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<header class="jumbotron hero-spacer">
<asp:Button ID="BackToTasks" runat="server" Text="Back" OnClick="BackToTasks_Click" CssClass="btn btn-primary btn-large" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="GrabAssignedTasks" Height="179px" Width="829px" OnSelectedIndexChanged="HoursChange_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Story" HeaderText="Story" SortExpression="Story" />
<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" />
<asp:BoundField DataField="EstimatedHours" HeaderText="Estimated Hours" SortExpression="EstimatedHours" />
<asp:BoundField DataField="UserStoryTaskID" HeaderText="UserStoryTaskID" SortExpression="UserStoryTaskID" InsertVisible="False" ReadOnly="True" HeaderStyle-CssClass="hidden-field" ItemStyle-CssClass="hidden-field"/>
<asp:BoundField DataField="TaskLogID" HeaderText="TaskLogID" SortExpression="TaskLogID" InsertVisible="False" ReadOnly="True" HeaderStyle-CssClass="hidden-field" ItemStyle-CssClass="hidden-field"/>
<asp:BoundField DataField="HoursLeft" HeaderText="Hours Left" SortExpression="HoursLeft" />
<asp:BoundField DataField="ModifiedDate" HeaderText="Last Updated" SortExpression="ModifiedDate" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>
</header>
我在 C# 代码中对此没有任何具体说明。
这是我正在使用的数据库中 table 的图片。 Database table
您没有加入 Table Tasks_Log 和 Tasks_Log_1,所以会发生的是 Tasks_Log 中只有修改日期匹配“30/11/ 2015" 得到返回,不管任务。在 Tasks_Log_1 的 WHERE 子句中添加一个条目,以确保您也在正确的 TaskID 上进行匹配:
SELECT Backlog.Story, Tasks.Task, Tasks.EstimatedHours, Tasks.UserStoryTaskID, Tasks_Log.TaskLogID, Tasks_Log.HoursLeft, Tasks_Log.ModifiedDate
FROM Backlog
INNER JOIN Tasks ON Backlog.UserStoryID = Tasks.UserStoryID
INNER JOIN Tasks_Log ON Tasks.UserStoryTaskID = Tasks_Log.TaskID
WHERE (Tasks.OwnershipID = @UserID)
AND (Tasks_Log.ModifiedDate =
(SELECT MAX(ModifiedDate)
FROM Tasks_Log AS Tasks_Log_1
WHERE Tasks_Log_1.TaskID = Tasks_Log.TaskID))