Order By clause generated incorrectly