Fixed some sql errors for the aib queries