Merged changes and changed db query for agg sql