Check where count before merging nested wheres. Fixes #1338.