“Simple” SQL Query

前端 未结 5 1077
独厮守ぢ
独厮守ぢ 2020-12-20 02:58

Each of my clients can have many todo items and every todo item has a due date.

What would be the query for discovering the next undone todo item by due date for eac

5条回答
  •  伪装坚强ぢ
    2020-12-20 03:23

    The following should get you close, first get the min time for each client, then lookup the client/todo information

    SELECT
        C.Id,
        C.Name,
        T.Id
        T.Description,
        T.timestamp_due
    FROM
    {
        SELECT
            client_id,
            MIN(timestamp_due) AS "DueDate"
        FROM todos
        WHERE timestamp_completed IS NULL
        GROUP BY ClientId
    } AS MinValues
        INNER JOIN Clients C
        ON (MinValues.client_id = C.Id)
        INNER JOIN todos T
        ON (MinValues.client_id = T.client_id
            AND MinValues.DueDate = T.timestamp_due)
    ORDER BY C.Name
    

    NOTE: Written assuming SQL Server

提交回复
热议问题