sql case 与 sum
<select id="selectTotal" resultType="java.util.Map" parameterType="java.util.Map" statementType="STATEMENT"> SELECT SUM(CASE way WHEN 'ZFB' THEN money_amount ELSE 0 END ) ZFBSUM, SUM(CASE way WHEN 'ZFBWAP' THEN money_amount ELSE 0 END ) ZFBWAPSUM, SUM(CASE way WHEN 'WX' THEN money_amount ELSE 0 END ) WXSUM, SUM(CASE way WHEN 'WXWAP' THEN money_amount ELSE 0 END ) WXWAPSUM, SUM(CASE way WHEN 'ZFB' THEN 1 ELSE 0 END ) ZFB, SUM(CASE way WHEN 'ZFBWAP' THEN 1 ELSE 0 END ) ZFBWAP, SUM(CASE way WHEN 'WX' THEN 1 ELSE 0 END ) WX, SUM(CASE way WHEN 'WXWAP' THEN 1 ELSE 0 END ) WXWAP FROM ${tableName} WHERE the_super_id = '${the_super_id}'; </select>
Map<String, Long> re = sqlSession.selectOne(MybatisNamespace.ordersTemp_selectTotal, param); Map<String, String> foIn = new LinkedHashMap<>(); foIn.put("tId", tId); foIn.put("ZFBSUM", re.get("ZFBSUM") + ""); foIn.put("ZFBWAPSUM", re.get("ZFBWAPSUM") + ""); foIn.put("WXSUM", re.get("WXSUM") + ""); foIn.put("WXWAPSUM", re.get("WXWAPSUM") + ""); foIn.put("ZFB", re.get("ZFB") + ""); foIn.put("ZFBWAP", re.get("ZFBWAP") + ""); foIn.put("WX", re.get("WX") + ""); foIn.put("WXWAP", re.get("WXWAP") + ""); foIn.put("remainder_update_time", new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date().getTime()));