I'm trying to write a query for an online coding event website, but I'm finding it particularly difficult to deal with. 

I have a table submissions:

```
submissions => subid | uid | pid | subts | status

uid = userid
pid = problem id
subts = timestamp when the submission was submitted
status = whether the answer is right or not

```

A user might have made multiple submissions for a given pid.

I want to find out: who was the latest user to submit his solution for every problem?

For now, I don't care if the solution was right or wrong.

The query I though would work was

```
select pid, uid, max(subts) from submissions group by pid;

```

but its not working the way I want it to. This query finds the maximum ts alright, but the uid associated with it not correct.

Can anyone teach me what's wrong with my query? and what is the right way to write a query for my purpose?

Thanks

Note: I'm using mysql.

Edit: I could do this by iterating over all the pids, and writing

```
select pid, uid, max(subts) from submissions where pid=$pid order by subts desc limit 1;

```

But I don't really want to do this. I want to know if a single query can accomplish what I want. If yes, I want to know how.