Improve SQL generation for reductions and By on reductions

Our SQLAlchemy generation continues to be broken.  Here are two failing tests.  We should probably fix these soon.