fixes to SQL ordering bugs
Merge pull request #183 from rwhaling/master

fixes to SQL ordering bugs