fixed sql for building and ground arrays