String sql = "SELECT k.cust_id, market, k.no_order_follow_up, k.no_potential_contacts\n" + " FROM kg_manual_frequency_options k\n"; if (includedMMUsers != null) { sql = sql + " JOIN user_mst u ON u.user_id = c.slspers_id\n"; } sql = sql + " WHERE (no_order_follow_up = 1 OR no_potential_contacts = 1)"; if (includedMMUsers != null) { StringBuffer sqlBuffer = new StringBuffer(sql); String kg_manual_frequency_options = "kg_manual_frequency_options k"; sqlBuffer.insert(sqlBuffer.indexOf(kg_manual_frequency_options) + kg_manual_frequency_options.length(), ", cust_per_category c"); sqlBuffer = sqlBuffer.append("\n"). append(" AND c.cust_id = k.cust_id\n"). append(" AND DECODE (c.CATEGORY, 'J', 0, 'O', 1) = k.market\n"). append(" AND u.mm_user_id IN (").append(getCommaSeparated()).append(')'); sql = sqlBuffer.toString(); }