日期:2014-05-16  浏览次数:20889 次

特急 关于多表合计
把本来分开写的五段SQL,合成一段,但检索结果不对,结果是翻倍的,应该如何写?
合并后的sql
  SB.Append(" SELECT ");
  SB.Append(" count(aff.SINSA_KEKKA), ");
  SB.Append(" sum(asp1.DAT_VALUE), ");
  SB.Append(" sum(asp2.DAT_VALUE), ");
  SB.Append(" count(client1.MOSHI_KB), ");
  SB.Append(" count(client2.SEIYAKU_KB), ");
  SB.Append(" aspm.NAME as NAME, ");
  SB.Append(" aspm.CARRIER as CARRIER, ");
  SB.Append(" client2.HASEI_DATE as HASEI_DATE, ");
  SB.Append(" client2.PRO_NAME as PRO_NAME ");

  SB.Append(" FROM ");
  SB.Append(" aff_sites aff, ");
  SB.Append(" asp_dailys asp1, ");
  SB.Append(" asp_dailys asp2, ");
  SB.Append(" client_results client1, ");
  SB.Append(" client_results client2, ");
  SB.Append(" asp_mst aspm ");


  SB.Append(" WHERE ");
  SB.Append(" aff.CLIENT_ID ");
  SB.Append(" = ");
  SB.Append(" @client_id ");
  SB.Append(" AND ");
  //プロモーションが「指定なし」以外
  if (!Const.SITEINASI_NAYO.Equals(W002007_VO.txtPAGE_NM))
  {
  SB.Append("aff.PRO_NAME");
  SB.Append(" = ");
  SB.Append(" @pro_name ");
  }
  else
  {
  SB.Append("(1 = 1)");
  }
  SB.Append(" AND ");
  //ASPが「指定なし」以外
  if (!Const.SITEINASI_NAYO.Equals(W002007_VO.txtASP_NM))
  {
  SB.Append("aff.ASP_ID");
  SB.Append(" = ");
  SB.Append(" @asp_id ");
  }
  else
  {
  SB.Append("(1 = 1)");
  }
  SB.Append(" AND ");
  SB.Append("(");
  //From入力した
  if (!"".Equals(W002007_VO.txtDATE_FROM.Trim()))
  {
  SB.Append("aff.SINSA_DATE");
  SB.Append(" >= ");
  SB.Append("@DATE_From");
  }
  else
  {
  SB.Append(" 1=1 ");
  }
  SB.Append(")");

  SB.Append(" AND ");

  SB.Append("(");

  //To入力した
  if (!"".Equals(W002007_VO.txtDATE_TO.Trim()))
  {
  SB.Append("aff.SINSA_DATE");
  SB.Append(" <= ");
  SB.Append("@DATE_To");
  }
  else
  {
  SB.Append(" 1=1 ");
  }

  SB.Append(")");

  SB.Append(" AND ");
  SB.Append(" aff.SINSA_KEKKA ");
&