build huge view to construct a sql query