Some SQL fixes
Forgot a THEN
Merge pull request #1 from Quatrerwin/q-branch

Some SQL fixes from tyler