Fixed issue with colliding id values in sql query