How to convert this sql statement