Mysql – Find records that do not have matching closing value

MySQL

Sorry for unclear title, i am having trouble formulating the right question subject.
Hopefully the details will help you understand more clearly the issue i am facing.

I have a table with the following schema:

+----+----------+-----+
| ID |  Status  | PID | Time ...
+----+----------+-----+---
|  1 |  STARTED |  10 |
|  2 |  STARTED |  12 |
|  3 |  ENDED   |  10 |
|  4 |  STARTED |  18 |
|  5 |  STARTED |  21 |
|  6 |  ENDED   |  12 |
+----+----------+-----+

Every time a process starts, an entry containing its PID set to 'STARTED' Status in database.
Every time a process finishes its job, another entry containing its PID gets written to the database with the Status value of 'ENDED'.
A process with same 'PID' can start once again after finish.

I would appreciate if someone can guide me in the right direction to a query which will return all the PID's that have started but not (yet) completed.

Desired output:

+----+----------+-----+
| ID |  Status  | PID | Time ...
+----+----------+-----+---
|  4 |  STARTED |  18 |
|  5 |  STARTED |  21 |
+----+----------+-----+

Best Answer

If the process with some PID was started, but was not finished, there is only one record for it.

SELECT pid
FROM table
GROUP BY pid
HAVING COUNT(pid) = 1

If process with the same PIDcan start/finish more than once, working process have odd records count.

HAVING 1 = (COUNT(pid) MOD 2)

Having PIDs list you can easily obtain the whole records.