Factorize rest-table and fix/simplify SQL