I have items which can be flagged with a status for a time range. In each row, I have the start and stop times for a item with a flag.

For example:

```
start   stop    itemid  flag
1-Jan   1-Feb   1       a
1-Feb   1-Mar   1       b
1-Feb   1-Mar   2       a
1-Jan   1-Mar   3       a
1-Jan   null    4       a

```

What I need is a subcount of a count. That is, for two dates given (say `1-Jan` and `1-Mar`), I need to count the number of items which had flag `a` at the first date but not flag `a` at the second date. (And other counts, with the other 3 combinations of `a` and not-`a` at those two dates.)

I also need to somehow account for no flag at either date (but not both), but for now I can assume that no flag is `b`.

Essentially, I need to count the following cases: (a,a), (a,b), (a,null), (b,a), (b,b), (b,null); for (x,y), where x is the flag for each item at the first date, and y is the flag at the second date.

Existing, I can count how many flags `a` or `b` exist for a date.

```
select count(1)
from table
where start <= '2014-02-15'
  and (stop >= '2014-02-15' or stop is null)
  and flag = a

```