oracle sql 日期 case when where
阿新 • • 發佈:2019-02-02
public String getMarkets(CrmTransBean crmTransBean) throws AppException{
StringBuilder sb = new StringBuilder();
sb.append("SELECT CA.ID, ");
sb.append(" CA.NAME, ");
sb.append(" TO_CHAR( CA.START_C,'yyyy-MM') MONDATE, ");
sb.append(" CC.NAME CREATENAME, ");
//活動開始時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.START_C,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.START_C,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END STARTTIME, ");
//活動結束時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.END_C,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.END_C,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END ENDTIME, ");
//預約開始時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.OPENDATE,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.OPENDATE,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END OPENTIME, ");
//預約結束時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.CLOSEDDATE,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.CLOSEDDATE,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END CLOSETIME, ");
sb.append(" CASE ");
sb.append(" WHEN CA.SORT_C = '年度鑽石年會' OR CA.SORT_C = 'PE類合夥人年會' ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END FC_CANBOOK, ");
sb.append(" CASE ");
sb.append(" WHEN CA.END_C < SYSDATE ");
sb.append(" THEN '已結束' ELSE '未結束' ");
sb.append(" END STATE, ");
sb.append(" CASE ");
sb.append(" WHEN CA.CREATEDATE > SYSDATE-7 ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END ISNEW, ");
sb.append(" CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END ISFIRST ");
sb.append(" FROM CAMPAIGN CA ");
sb.append(" JOIN CCUSER CC ");
sb.append(" ON CA.CREATEBYID = CC.ID ");
sb.append(" WHERE 1=1 ");
sb.append(" AND (( ");
sb.append(" CC.NAME != '資料互動使用者' AND CA.ZHUANGTAI IN('已完成','總部市場部批准','客服專案經理批准','活動結果已通過稽核','活動已提交,但稽核未通過','未開始','進行中','開放預約','結束預約') ");
sb.append(" AND CA.START_C IS NOT NULL AND CA.END_C IS NOT NULL AND CA.PITCH_AREA IS NOT NULL ");
sb.append(" AND (CA.RANGE_NEW LIKE '%"+crmTransBean.getBranchname()+"%' OR CA.RANGE_NEW LIKE '%全國%') ");
sb.append(" ) ");
sb.append(" OR ( ");
sb.append(" CC.NAME = '資料互動使用者' AND CA.PUBLISH_STATUS = '已釋出' AND CA.ZHUANGTAI != '活動無效' ");
sb.append(" AND ( CA.BRANCH LIKE '%"+crmTransBean.getBranchname()+"%' OR CA.BRANCH LIKE '%全國%') ");
sb.append(" )) ");
if(!"".equals(crmTransBean.getSearch()) && null != crmTransBean.getSearch()){
sb.append(" AND CA.NAME LIKE '%"+crmTransBean.getSearch()+"%' ");
}
if(null != crmTransBean.getIsfirst() && crmTransBean.getIsfirst().equals("Y")){
sb.append(" AND (CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END )='Y' ");
}
if(null != crmTransBean.getIsfirst() && crmTransBean.getIsfirst().equals("N")){
sb.append(" AND (CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END )='N' ");
}
if(null != crmTransBean.getMondate1() && !"".equals(crmTransBean.getMondate1()) && !"".equals(crmTransBean.getMondate2()) && null != crmTransBean.getMondate2()){
sb.append(" AND TO_CHAR(CA.START_C, 'yyyy-MM') IN ('"+crmTransBean.getMondate1()+"','"+crmTransBean.getMondate2()+"') ");
}
if(null != crmTransBean.getMondate1() && !"".equals(crmTransBean.getMondate1()) && ("".equals(crmTransBean.getMondate2()) || null == crmTransBean.getMondate2())){
sb.append(" AND TO_CHAR(CA.START_C, 'yyyy-MM') IN ('"+crmTransBean.getMondate1()+"') ");
}
sb.append("ORDER BY START_C DESC");
return sb.toString();
}
StringBuilder sb = new StringBuilder();
sb.append("SELECT CA.ID, ");
sb.append(" CA.NAME, ");
sb.append(" TO_CHAR( CA.START_C,'yyyy-MM') MONDATE, ");
sb.append(" CC.NAME CREATENAME, ");
//活動開始時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.START_C,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.START_C,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END STARTTIME, ");
//活動結束時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.END_C,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.END_C,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END ENDTIME, ");
//預約開始時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.OPENDATE,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.OPENDATE,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END OPENTIME, ");
//預約結束時間轉換
sb.append(" CASE ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.CLOSEDDATE,'mm-dd pmhh24:mi:ss'),'am','上午') ");
sb.append(" WHEN 1=1 ");
sb.append(" THEN REPLACE(TO_CHAR( CA.CLOSEDDATE,'mm-dd pmhh24:mi:ss'),'pm','下午') ");
sb.append(" ELSE '未填寫' ");
sb.append(" END CLOSETIME, ");
sb.append(" CASE ");
sb.append(" WHEN CA.SORT_C = '年度鑽石年會' OR CA.SORT_C = 'PE類合夥人年會' ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END FC_CANBOOK, ");
sb.append(" CASE ");
sb.append(" WHEN CA.END_C < SYSDATE ");
sb.append(" THEN '已結束' ELSE '未結束' ");
sb.append(" END STATE, ");
sb.append(" CASE ");
sb.append(" WHEN CA.CREATEDATE > SYSDATE-7 ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END ISNEW, ");
sb.append(" CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END ISFIRST ");
sb.append(" FROM CAMPAIGN CA ");
sb.append(" JOIN CCUSER CC ");
sb.append(" ON CA.CREATEBYID = CC.ID ");
sb.append(" WHERE 1=1 ");
sb.append(" AND (( ");
sb.append(" CC.NAME != '資料互動使用者' AND CA.ZHUANGTAI IN('已完成','總部市場部批准','客服專案經理批准','活動結果已通過稽核','活動已提交,但稽核未通過','未開始','進行中','開放預約','結束預約') ");
sb.append(" AND CA.START_C IS NOT NULL AND CA.END_C IS NOT NULL AND CA.PITCH_AREA IS NOT NULL ");
sb.append(" AND (CA.RANGE_NEW LIKE '%"+crmTransBean.getBranchname()+"%' OR CA.RANGE_NEW LIKE '%全國%') ");
sb.append(" ) ");
sb.append(" OR ( ");
sb.append(" CC.NAME = '資料互動使用者' AND CA.PUBLISH_STATUS = '已釋出' AND CA.ZHUANGTAI != '活動無效' ");
sb.append(" AND ( CA.BRANCH LIKE '%"+crmTransBean.getBranchname()+"%' OR CA.BRANCH LIKE '%全國%') ");
sb.append(" )) ");
if(!"".equals(crmTransBean.getSearch()) && null != crmTransBean.getSearch()){
sb.append(" AND CA.NAME LIKE '%"+crmTransBean.getSearch()+"%' ");
}
if(null != crmTransBean.getIsfirst() && crmTransBean.getIsfirst().equals("Y")){
sb.append(" AND (CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END )='Y' ");
}
if(null != crmTransBean.getIsfirst() && crmTransBean.getIsfirst().equals("N")){
sb.append(" AND (CASE ");
sb.append(" WHEN CA.END_C > SYSDATE AND CA.CLOSEDDATE > SYSDATE ");
sb.append(" THEN 'Y' ELSE 'N' ");
sb.append(" END )='N' ");
}
if(null != crmTransBean.getMondate1() && !"".equals(crmTransBean.getMondate1()) && !"".equals(crmTransBean.getMondate2()) && null != crmTransBean.getMondate2()){
sb.append(" AND TO_CHAR(CA.START_C, 'yyyy-MM') IN ('"+crmTransBean.getMondate1()+"','"+crmTransBean.getMondate2()+"') ");
}
if(null != crmTransBean.getMondate1() && !"".equals(crmTransBean.getMondate1()) && ("".equals(crmTransBean.getMondate2()) || null == crmTransBean.getMondate2())){
sb.append(" AND TO_CHAR(CA.START_C, 'yyyy-MM') IN ('"+crmTransBean.getMondate1()+"') ");
}
sb.append("ORDER BY START_C DESC");
return sb.toString();
}