SQL按日期显示每个任务的最新记录

时间:2015-11-30 19:56:32

标签: html mysql date sqldatasource sqlcommand

我想显示每项任务的最新更新记录。 目前,无论做什么任务,我都只获得最新的记录。

这是前端代码

<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#代码中没有任何具体内容。

这是我正在使用的数据库中的表格的图片。 Database table

1 个答案:

答案 0 :(得分:1)

您没有使用Tasks_Log_1加入Table Tasks_Log,所以会发生的情况是只有在Tasks_Log中修改日期匹配&#34; 30/11 / 2015&#34;无论任务如何,都会得到回报。在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))