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

<p>What would be the query for discovering the next undone todo item by due date for each file?  In the event that a client has more than one todo, the one with the lowest id is the correct one.</p>

<p>Assuming the following minimal schema:</p>

<pre><code>clients (id, name)

todos (id, client_id, description, timestamp_due, timestamp_completed)
</code></pre>

<p>Thank you.</p>

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