首页 > 代码库 > 性能改善后复杂SQL

性能改善后复杂SQL

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="jp.co.asahi_np.biz.bbs.pcs.dao.mik.ShukoMikomiDao">
 <!-- グループ別掲載社別(NET売上) 202 -->
 <select id="selectMikomiFee" parameterType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  SELECT shukeiGroupCode,
         shukeiGroupName,
         CASE
             WHEN (mikomiFeeT IS NOT NULL OR mikomiFeeO IS NOT NULL OR
                  mikomiFeeS IS NOT NULL OR mikomiFeeN IS NOT NULL OR
                  mikomiFeeH IS NOT NULL) THEN
       round((nvl(mikomiFeeT,0)+nvl(mikomiFeeO,0)+nvl(mikomiFeeS,0)+nvl(mikomiFeeN,0)+nvl(mikomiFeeH,0))/1000,0)
             ELSE
             round((mikomiFeeT+mikomiFeeO+mikomiFeeS+mikomiFeeN+mikomiFeeH)/1000,0)
         END AS mikomiFeeSum,
    round(mikomiFeeT/1000,0) as mikomiFeeT,
          round(mikomiFeeO/1000,0) as mikomiFeeO,
          round(mikomiFeeS/1000,0) as mikomiFeeS,
          round(mikomiFeeN/1000,0) as mikomiFeeN,
          round(mikomiFeeH/1000,0) as mikomiFeeH,
         numT,
         numO,
         numS,
         numN,
         numH,
          dspJyun
  FROM   (
    SELECT shukeiGroupCode AS shukeiGroupCode,
           shukeiGroupName AS shukeiGroupName,
           sum(T) AS mikomiFeeT,
           sum(O) AS mikomiFeeO,
           sum(S) AS mikomiFeeS,
           sum(N) AS mikomiFeeN,
           sum(H) AS mikomiFeeH,
           atom_concat(numT) AS numT,
                 atom_concat(numO) AS numO,
                 atom_concat(numS) AS numS,
                 atom_concat(numN) AS numN,
                 atom_concat(numH) AS numH,
                  dspJyun
    FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                   tsg.shukei_group_name AS shukeiGroupName,
                    tsg.dsp_jyun as dspJyun,
                   CASE
                             WHEN tsm.ksai_sha_code = ‘1‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numT,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘2‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numO,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘3‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numS,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘4‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numN,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘5‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numH,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘1‘ THEN
                        tsm.mikomi_fee
                   END AS T,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘2‘ THEN
                        tsm.mikomi_fee
                   END AS O,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘3‘ THEN
                        tsm.mikomi_fee
                   END AS S,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘4‘ THEN
                        tsm.mikomi_fee
                   END AS N,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘5‘ THEN
                        tsm.mikomi_fee
                   END AS H
            FROM   tb_shuko_mikomi        tsm, --出稿見込情報
                   tb_shukei_group        tsg, --集計グループ
                   tb_shukei_adv_relation tsar --集計グループ広告媒体関連
            WHERE  tsm.del_flg(+) = ‘0‘
            AND    tsg.del_flg = ‘0‘
            AND    tsar.del_flg = ‘0‘
            AND    tsm.uriage_nengetsu(+) = replace(‘2014/06‘,‘/‘,‘‘) --売上年月
      AND    tsm.uriage_sha_code(+) = ‘1‘ --売上社コード
      AND    tsm.tanto_bu_ka_code(+) = ‘110‘ --担当部課コード
      AND    tsm.gyosyu_mcode(+) = ‘10‘ --業種中分類コード
            AND    tsm.shukei_group_code(+) = tsg.shukei_group_code
            AND    tsg.shukei_group_kubun IN (‘1‘, ‘2‘)
            AND    tsg.shukei_group_code = tsar.shukei_group_code
            AND    tsar.input_flg = ‘1‘ --入力可否フラグ
            GROUP  BY tsg.shukei_group_code,
                      tsg.shukei_group_name,
                      tsm.ksai_sha_code,
                      tsm.mikomi_fee,
                      tsm.shuko_mikomi_mgr_num_temp,
                       tsg.dsp_jyun
            ORDER  BY tsg.dsp_jyun)
    GROUP  BY shukeiGroupCode,
              shukeiGroupName,
              dspJyun)
  ]]>
 </select>
 
 <!-- グループ別掲載社別(段数) 203 -->
 <select id="selectMikomiDansu" parameterType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  SELECT shukeiGroupCode,
         shukeiGroupName,
         CASE
             WHEN (mikomiDansuT IS NOT NULL OR mikomiDansuO IS NOT NULL OR
                  mikomiDansuS IS NOT NULL OR mikomiDansuN IS NOT NULL OR
                  mikomiDansuH IS NOT NULL) THEN
              nvl(mikomiDansuT, 0) + nvl(mikomiDansuO, 0) +
              nvl(mikomiDansuS, 0) + nvl(mikomiDansuN, 0) +
              nvl(mikomiDansuH, 0)
             ELSE
              mikomiDansuT + mikomiDansuO + mikomiDansuS + mikomiDansuN +
              mikomiDansuH
         END AS mikomiFeeSum,
         mikomiDansuT,
         mikomiDansuO,
         mikomiDansuS,
         mikomiDansuN,
         mikomiDansuH,
         numT,
         numO,
         numS,
         numN,
         numH,
           dspJyun
  FROM   (
    SELECT shukeiGroupCode AS shukeiGroupCode,
           shukeiGroupName AS shukeiGroupName,
           sum(T) AS mikomiDansuT,
           sum(O) AS mikomiDansuO,
           sum(S) AS mikomiDansuS,
           sum(N) AS mikomiDansuN,
           sum(H) AS mikomiDansuH,
           atom_concat(numT) AS numT,
                 atom_concat(numO) AS numO,
                 atom_concat(numS) AS numS,
                 atom_concat(numN) AS numN,
                 atom_concat(numH) AS numH,
                   dspJyun
    FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                   tsg.shukei_group_name AS shukeiGroupName,
                   tsg.dsp_jyun as dspJyun,
                   CASE
                             WHEN tsm.ksai_sha_code = ‘1‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numT,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘2‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numO,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘3‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numS,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘4‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numN,
                         CASE
                             WHEN tsm.ksai_sha_code = ‘5‘ THEN
                              tsm.shuko_mikomi_mgr_num_temp
                         END AS numH,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘1‘ THEN
                        --tsm.mikomi_dansu
                        ROUND(tsm.mikomi_dansu/100, 2)
                   END AS T,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘2‘ THEN
                        --tsm.mikomi_dansu
                        ROUND(tsm.mikomi_dansu/100, 2)
                   END AS O,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘3‘ THEN
                        --tsm.mikomi_dansu
                        ROUND(tsm.mikomi_dansu/100, 2)
                   END AS S,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘4‘ THEN
                        --tsm.mikomi_dansu
                        ROUND(tsm.mikomi_dansu/100, 2)
                   END AS N,
                   CASE
                       WHEN tsm.ksai_sha_code = ‘5‘ THEN
                       -- tsm.mikomi_dansu
                        ROUND(tsm.mikomi_dansu/100, 2)
                   END AS H
            FROM   tb_shuko_mikomi        tsm, --出稿見込情報
                   tb_shukei_group        tsg, --集計グループ
                   tb_shukei_adv_relation tsar --集計グループ広告媒体関連
            WHERE  tsm.del_flg(+) = ‘0‘
            AND    tsg.del_flg = ‘0‘
            AND    tsar.del_flg = ‘0‘
            AND    tsm.uriage_nengetsu(+) = replace(‘2014/06‘,‘/‘,‘‘) --売上年月
            AND    tsm.uriage_sha_code(+) = ‘1‘ --売上社コード
            AND    tsm.tanto_bu_ka_code(+) = ‘110‘ --担当部課コード
            AND    tsm.gyosyu_mcode(+) = ‘10‘ --業種中分類コード
            AND    tsm.shukei_group_code(+) = tsg.shukei_group_code
            AND    tsg.shukei_group_kubun IN (‘1‘, ‘2‘)
            AND    tsg.shukei_group_code = tsar.shukei_group_code
            AND    tsar.dansu_input_flg = ‘1‘ --段数入力可否フラグ
            GROUP  BY tsg.shukei_group_code,
                      tsg.shukei_group_name,
                      tsm.ksai_sha_code,
                      tsm.mikomi_dansu,
                      tsm.shuko_mikomi_mgr_num_temp,
                      tsg.dsp_jyun
            ORDER  BY tsg.dsp_jyun)
    GROUP  BY shukeiGroupCode,
              shukeiGroupName,
              dspJyun)
  ]]>
 </select>
 
 <!-- 405 -->
 <!-- 当月売上計 (NET売上)-->
 <select id="selectFee405" parameterType ="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  with innerView as (
  SELECT/*+materialize*/kumi_item_mgmt_num,
     shukei_group_code
                 FROM   (
                                 SELECT tmkii.kumi_item_mgmt_num, --申込組込時項目情報.申込組込時項目管理番号
                                       tsar.shukei_group_code, --集計グループ広告媒体関連.集計グループコード
                                       ROW_NUMBER() OVER(PARTITION BY tmkii.receipt_num, tmkii.ksai_sha_code ORDER BY tmkii.dsp_jyun ASC, tmkii.hansu_code DESC) r

                                FROM   tb_moko_kumi_item_info tmkii,
                                       tb_shukei_adv_relation tsar
                                WHERE  1=1
                                ]]>
                                AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) IN
                                <if test="nengetsuList != null and nengetsuList.size() > 0">
                                 <foreach collection="nengetsuList" item="item"
          open="(" separator="," close=")">
          #{item}
         </foreach>
                                </if>
                                <![CDATA[
                                AND    EXISTS
                                 (SELECT 1
                                        FROM  
                                            tb_moko_info_ksai_sha_betsu tmiksb,
                                            tb_moko_info_matching       tmim,
                                            tb_moko_info_basic          tmib
                                              
                                        WHERE
                                            tmib.receipt_num = tmiksb.receipt_num
                                        AND    tmib.his = tmiksb.his
                                       
                                        AND ((tmib.yoyaku_waku_kubun_code IS NOT NULL AND
                                               tmkii.receipt_num = tmib.yoyaku_waku_receipt_num) OR
                                               (tmib.yoyaku_waku_kubun_code IS NULL AND
                                               tmkii.receipt_num = tmib.receipt_num) OR
                             (tmkii.receipt_num = tmim.genko_umeawase_id AND
                             tmib.receipt_num = tmim.receipt_num AND
                              tmim.his= tmib.his ))
                       AND   tmiksb.ksai_sha_code = tmkii.ksai_sha_code
                                         AND   tmiksb.jisseki_sign  = tmkii.jisseki_sign
                                         AND   tmiksb.del_flg = ‘0‘
                      AND    tmib.moko_kubun_code <> ‘1‘
                      AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                      AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tmib.del_flg = ‘0‘
                                        AND    tmib.yuko_sign = ‘2‘)
                                     
                                AND    (tmkii.han_keiretsu_code NOT IN
                                      ]]>
  <foreach collection="hanKeiretsuCodeList" item="item" open="(" separator="," close=")">
         #{item.codeValue}
        </foreach>
        <![CDATA[
                                OR tmkii.han_keiretsu_code IS NULL)
                                AND    tmkii.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                                AND    (CASE
                                            WHEN EXISTS (SELECT 1
                                                  FROM   tb_shukei_adv_relation tsar
                                                  WHERE  tmkii.adv_kind_lcode = tsar.adv_kind_lcode
                                                  AND    tmkii.adv_baitai_bunrui_code =
                                                         tsar.adv_baitai_bunrui_code
                                                  AND    tsar.count_flg = ‘1‘
                                                  AND    tsar.input_flg = ‘1‘) THEN
                                             tmkii.adv_kind_lcode
                                            ELSE
                                             ‘*‘
                                        END) = tsar.adv_kind_lcode
                                AND    tsar.count_flg = ‘1‘
                                AND    tsar.input_flg = ‘1‘
                                AND    tmkii.del_flg = ‘0‘)
                 WHERE  r = 1),
        innerView2 as (
  SELECT/*+materialize*/kumi_item_mgmt_num,
     shukei_group_code
                 FROM   (
                                 SELECT tmkii.kumi_item_mgmt_num, --申込組込時項目情報.申込組込時項目管理番号
                                       tsar.shukei_group_code, --集計グループ広告媒体関連.集計グループコード
                                       ROW_NUMBER() OVER(PARTITION BY tmkii.receipt_num, tmkii.ksai_sha_code ORDER BY tmkii.ksai_date ASC) r

                                FROM   tb_moko_kumi_item_info tmkii,
                                       tb_shukei_adv_relation tsar
                                WHERE  1=1
                                ]]>
                                AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) IN
                                <if test="nengetsuList != null and nengetsuList.size() > 0">
                                 <foreach collection="nengetsuList" item="item"
          open="(" separator="," close=")">
          #{item}
         </foreach>
                                </if>
                                <![CDATA[
                                AND    EXISTS
                                 (SELECT 1
                                        FROM  
                                        tb_moko_info_ksai_sha_betsu tmiksb,
                                               tb_moko_info_matching       tmim,
                                               tb_moko_info_basic          tmib
                                        WHERE 
                                        tmib.receipt_num = tmiksb.receipt_num
                                        AND    tmib.his = tmiksb.his
                                        AND    ((tmib.yoyaku_waku_kubun_code IS NOT NULL AND
                                               tmkii.receipt_num = tmib.yoyaku_waku_receipt_num) OR
                                               (tmib.yoyaku_waku_kubun_code IS NULL AND
                                               tmkii.receipt_num = tmib.receipt_num) OR
                             (tmkii.receipt_num = tmim.genko_umeawase_id AND
                              tmim.receipt_num = tmib.receipt_num AND
                              tmim.his= tmib.his))
                      AND    tmiksb.ksai_sha_code = tmkii.ksai_sha_code
                                        AND    tmiksb.jisseki_sign = tmkii.jisseki_sign
                                        AND    tmiksb.del_flg = ‘0‘
                      AND    tmib.moko_kubun_code <> ‘1‘
                      AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.moko_kind = ‘30‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tmib.del_flg = ‘0‘
                                        AND    tmib.yuko_sign = ‘2‘
                                        )
                                     
                                AND    (tmkii.han_keiretsu_code NOT IN
                                      ]]>
  <foreach collection="hanKeiretsuCodeList" item="item" open="(" separator="," close=")">
         #{item.codeValue}
        </foreach>
        <![CDATA[
                                OR tmkii.han_keiretsu_code IS NULL)
                                AND    tmkii.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                                AND    (CASE
                                            WHEN EXISTS (SELECT 1
                                                  FROM   tb_shukei_adv_relation tsar
                                                  WHERE  tmkii.adv_kind_lcode = tsar.adv_kind_lcode
                                                  AND    tmkii.adv_baitai_bunrui_code =
                                                         tsar.adv_baitai_bunrui_code
                                                  AND    tsar.count_flg = ‘1‘
                                                  AND    tsar.input_flg = ‘1‘) THEN
                                             tmkii.adv_kind_lcode
                                            ELSE
                                             ‘*‘
                                        END) = tsar.adv_kind_lcode
                                AND    tsar.count_flg = ‘1‘
                                AND    tsar.input_flg = ‘1‘
                                AND    tmkii.del_flg = ‘0‘)
                 WHERE  r = 1)
  
  select * from (SELECT shukeiGroupCode,
         shukeiGroupName,
         CASE
             WHEN (mikomiFeeT IS NOT NULL OR mikomiFeeO IS NOT NULL OR
                  mikomiFeeS IS NOT NULL OR mikomiFeeN IS NOT NULL OR
                  mikomiFeeH IS NOT NULL) THEN
          (nvl(mikomiFeeT,0)+nvl(mikomiFeeO,0)+nvl(mikomiFeeS,0)+nvl(mikomiFeeN,0)+nvl(mikomiFeeH,0))
             ELSE
             (mikomiFeeT+mikomiFeeO+mikomiFeeS+mikomiFeeN+mikomiFeeH)
         END AS mikomiFeeSum,
         mikomiFeeT as mikomiFeeT,
         mikomiFeeO as mikomiFeeO,
         mikomiFeeS as mikomiFeeS,
         mikomiFeeN as mikomiFeeN,
         mikomiFeeH as mikomiFeeH
  FROM   (
    SELECT shukeiGroupCode AS shukeiGroupCode,
           shukeiGroupName AS shukeiGroupName,
           sum(T) AS mikomiFeeT,
           sum(O) AS mikomiFeeO,
           sum(S) AS mikomiFeeS,
           sum(N) AS mikomiFeeN,
           sum(H) AS mikomiFeeH
    FROM   (SELECT shukeiGroupCode,
                   shukeiGroupName,
                   CASE
                       WHEN ksaiShaCode = ‘1‘ THEN
                        netFee
                   END AS T,
                   CASE
                       WHEN ksaiShaCode = ‘2‘ THEN
                        netFee
                   END AS O,
                   CASE
                       WHEN ksaiShaCode = ‘3‘ THEN
                        netFee
                   END AS S,
                   CASE
                       WHEN ksaiShaCode = ‘4‘ THEN
                        netFee
                   END AS N,
                   CASE
                       WHEN ksaiShaCode = ‘5‘ THEN
                        netFee
                   END AS H
            FROM   (SELECT shukeiGroupCode,
                           shukeiGroupName,
                           ksaiShaCode,
                           SUM(netFee) AS netFee
                    FROM   (
                            --405a
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                    tsg.shukei_group_code AS shukeiGroupCode,
                                    tsg.shukei_group_name AS shukeiGroupName,
                                    SUM(tfib.net_fee) AS netFee
                            FROM   tb_moko_info_basic     tmib,
                                    tb_fee_info_basic      tfib,
                                    tb_moko_kumi_item_info tmkii,
                                    tb_shukei_group        tsg,
                                    innerView
                            WHERE  tmib.receipt_num = tfib.receipt_num
                            AND    tmib.his = tfib.his
                            AND    tmkii.ksai_sha_code = tfib.ksai_sha_code
                            AND    innerView.shukei_group_code = tsg.shukei_group_code
                            AND    tmkii.kumi_item_mgmt_num = innerView.kumi_item_mgmt_num
                            AND    ((tmib.yoyaku_waku_kubun_code IN (‘1‘,‘3‘,‘5‘) AND
                                  tmkii.receipt_num =
                                  tmib.yoyaku_waku_receipt_num) OR
                                  (tmib.yoyaku_waku_kubun_code IS NULL AND
                                  tmkii.receipt_num = tmib.receipt_num))
                            AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                            AND    tmib.moko_kubun_code <> ‘1‘
                            AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                            AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                            AND    tmkii.del_flg = ‘0‘
                            AND    tmib.del_flg = ‘0‘
                            AND    tfib.yuko_sign = ‘2‘
                            AND    tfib.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tfib.ksai_sha_code,
                                       tsg.shukei_group_code,
                                       tsg.shukei_group_name
                          
                           UNION ALL
                          
                           --405a--原稿MATCHING
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                    tsg.shukei_group_code AS shukeiGroupCode,
                                    tsg.shukei_group_name AS shukeiGroupName,
                                    SUM(tfib.net_fee) AS netFee
                            FROM   tb_moko_info_basic     tmib,
                                    tb_fee_info_basic      tfib,
                                    tb_moko_kumi_item_info tmkii,
                                    tb_shukei_group        tsg,
                                    innerView
                            WHERE  tmib.receipt_num = tfib.receipt_num
                            AND    tmib.his = tfib.his
                            AND    tmkii.ksai_sha_code = tfib.ksai_sha_code
                            AND    innerView.shukei_group_code =
                                   tsg.shukei_group_code
                            AND    tmkii.kumi_item_mgmt_num =
                                   innerView.kumi_item_mgmt_num
                            AND   EXISTS
                                               (SELECT 1
                                                 FROM   tb_moko_info_matching tmim
                                                 WHERE  tmkii.receipt_num =
                                                        tmim.genko_umeawase_id
                                                 AND    tmim.receipt_num =
                                                        tmib.receipt_num
                                                 AND  tmim.his= tmib.his)
                                        AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.moko_kubun_code <> ‘1‘
                            AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                            AND    tmib.karihan_sign = ‘0‘
                            AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                            AND    tmib.del_flg = ‘0‘
                            AND    tfib.del_flg = ‘0‘
                            AND    tmkii.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            AND    tfib.yuko_sign = ‘2‘
                            GROUP  BY tfib.ksai_sha_code,
                                       tsg.shukei_group_code,
                                       tsg.shukei_group_name
                                       
                            UNION ALL
                           
                            --405b
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                   tsg.shukei_group_code AS shukeiGroupCode,
                                   tsg.shukei_group_name AS shukeiGroupName,
                                   SUM(tfib.net_fee) AS netFee
                            FROM   tb_moko_info_basic     tmib,
                                   tb_fee_info_basic      tfib,
                                   tb_moko_kumi_item_info tmkii,
                                   tb_shukei_group        tsg,
                                   innerView2
                            WHERE 
                                   tmkii.kumi_item_mgmt_num =
                                   innerView2.kumi_item_mgmt_num
                            AND    tmib.receipt_num = tfib.receipt_num
                            AND    tmib.his = tfib.his
                            AND    tmkii.ksai_sha_code = tfib.ksai_sha_code
                            AND    innerView2.shukei_group_code =
                                   tsg.shukei_group_code
                            AND    ((tmib.yoyaku_waku_kubun_code IN (‘1‘,‘3‘,‘5‘) AND
                                  tmkii.receipt_num =
                                  tmib.yoyaku_waku_receipt_num) OR
                                  (tmib.yoyaku_waku_kubun_code IS NULL AND
                                  tmkii.receipt_num = tmib.receipt_num))
                            AND    tmib.moko_kubun_code <> ‘1‘
                            AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.moko_kind  = ‘30‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                      AND    tfib.yuko_sign = ‘2‘
                      AND    tmib.del_flg = ‘0‘
                            AND    tfib.del_flg = ‘0‘
                            AND    tmkii.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tfib.ksai_sha_code,
                                      tsg.shukei_group_code,
                                      tsg.shukei_group_name
                           
                            UNION ALL
                           
                            --405b--原稿MATCHING
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                   tsg.shukei_group_code AS shukeiGroupCode,
                                   tsg.shukei_group_name AS shukeiGroupName,
                                   SUM(tfib.net_fee) AS netFee
                            FROM   tb_moko_info_basic     tmib,
                                   tb_fee_info_basic      tfib,
                                   tb_moko_kumi_item_info tmkii,
                                   tb_shukei_group        tsg,
                                   innerView2
                            WHERE  tmib.receipt_num = tfib.receipt_num
                            AND    tmib.his = tfib.his
                            AND    tmkii.ksai_sha_code = tfib.ksai_sha_code
                            AND    innerView2.shukei_group_code =
                                   tsg.shukei_group_code
                            AND    tmkii.kumi_item_mgmt_num =
                                   innerView2.kumi_item_mgmt_num
                            AND   EXISTS
                                               (SELECT 1
                                                 FROM   tb_moko_info_matching tmim
                                                 WHERE  tmkii.receipt_num =
                                                        tmim.genko_umeawase_id
                                                 AND    tmim.receipt_num =
                                                        tmib.receipt_num
                                                 AND  tmim.his= tmib.his)
                                        AND    tmib.moko_kubun_code <> ‘1‘
                                        AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.moko_kind  = ‘30‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                      AND    tfib.yuko_sign = ‘2‘
                      AND    tmib.del_flg = ‘0‘
                            AND    tfib.del_flg = ‘0‘
                            AND    tmkii.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tfib.ksai_sha_code,
                                      tsg.shukei_group_code,
                                      tsg.shukei_group_name
                                     
                            UNION ALL
                            --405c
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                   tsg.shukei_group_code AS shukeiGroupCode,
                                   tsg.shukei_group_name AS shukeiGroupName,
                                   SUM(tfib.net_fee) AS netFee
                            FROM   tb_moko_info_basic          tmib, --申込情報(基本)
                                   tb_moko_info_ksai_sha_betsu tmiksb, --申込情報(掲載社別)
                                   tb_fee_info_basic           tfib, --料金情報(基本)
                                   tb_shukei_adv_relation      tsar, --集計グループ広告媒体関連
                                   tb_shukei_group             tsg --集計グループ
                            WHERE 
                                   tmib.receipt_num = tmiksb.receipt_num
                            AND    tmib.his = tmiksb.his
                            AND    tmiksb.receipt_num = tfib.receipt_num
                            AND    tmiksb.his = tfib.his
                            AND    tmiksb.ksai_sha_code = tfib.ksai_sha_code
                            AND    tsar.shukei_group_code = tsg.shukei_group_code
                            AND    tmib.adv_baitai_bunrui_code =
                                   tsar.adv_baitai_bunrui_code
                            AND    (tmib.adv_kind_lcode = tsar.adv_kind_lcode OR
                                  tsar.adv_kind_lcode = ‘*‘)
                            AND   ((to_char(tmib.ksai_kibo_start_date_time_t, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘1‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_o, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘2‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_s, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘3‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_n, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘4‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_h, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘5‘) )
          AND    tmib.moko_kubun_code <> ‘1‘
                            AND    tmib.moko_kind not in (‘12‘,‘19‘)
                            AND    tsar.count_flg = ‘1‘
                            AND    tsar.input_flg = ‘1‘
                            AND    tmib.yuko_sign = ‘2‘
                            AND    tmib.moko_state_code = ‘4‘
                            AND    tmib.karihan_sign = ‘0‘
                            AND    tmib.kumikomi_fuyo_sign <> ‘0‘
                            AND    tmib.yoyaku_waku_kubun_code IS NULL
                            AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                            AND    tfib.yuko_sign = ‘2‘
                            AND    tmib.del_flg = ‘0‘
                            AND    tmiksb.del_flg = ‘0‘
                            AND    tfib.del_flg = ‘0‘
                            AND    tsar.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tfib.ksai_sha_code,
                                      tsg.shukei_group_code,
                                      tsg.shukei_group_name
                           
                            UNION ALL
                            --405d
                            SELECT ksaiShaCode,
                                   shukeiGroupCode,
                                   shukeiGroupName,
                                   SUM(netFee) AS netFee
                            FROM   (
                                    --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(掲載社別) の場合
                                    SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                      tsg.shukei_group_code AS shukeiGroupCode,
                                            tsg.shukei_group_name AS shukeiGroupName,
                                            SUM(tfib.net_fee) AS netFee
                                    FROM   tb_moko_info_hanyou         tmih, --申込情報(汎用?基本)
                                            tb_fee_info_basic           tfib, --料金情報(基本)
                                            tb_shukei_adv_relation      tsar, --集計グループ広告媒体関連
                                            tb_shukei_group             tsg, --集計グループ
                                            tb_moko_info_ksai_sha_betsu tmiksb --申込情報(掲載社別)
                                    WHERE 
                                           tmih.receipt_num = tfib.receipt_num
                                    AND    tmih.his = tfib.his
                                    AND    tsar.shukei_group_code =
                                           tsg.shukei_group_code
                                    AND    tmih.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    (tmih.adv_kind_lcode =
                                          tsar.adv_kind_lcode OR
                                          tsar.adv_kind_lcode = ‘*‘)
                                    AND    tmih.receipt_num = tmiksb.receipt_num
                                    AND    tmih.his = tmiksb.his
                                    AND    tmiksb.ksai_sha_code = tfib.ksai_sha_code
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.input_flg = ‘1‘
                                    AND    tmih.yuko_sign = ‘2‘
                                    AND    tmih.moko_state_code = ‘4‘
                                    AND    tmih.moko_kubun_code <> ‘1‘
                                    AND    to_char(tmiksb.ksai_kibo_start_date_time, ‘YYYY/MM‘) =
                                           ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    tfib.yuko_sign = ‘2‘
                                    AND    tmih.del_flg = ‘0‘
                                    AND    tfib.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tmiksb.del_flg = ‘0‘
                                       and    tsg.del_flg=‘0‘
                                    GROUP  BY tfib.ksai_sha_code,
                                               tsg.shukei_group_code,
                                               tsg.shukei_group_name
                                   
                                    UNION ALL
                                    --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                            tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic         tfib, --料金情報(基本)
                                               tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                               tb_shukei_group           tsg, --集計グループ
                                               tb_moko_info_sub_m_detail tmismd --申込情報(サブメディア地域?見本紙別)
                                        WHERE 
                                               tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tmih.receipt_num =
                                               tmismd.receipt_num
                                        AND    tmih.his = tmismd.his
                                        AND    (tmismd.mihonshi_sign = ‘0‘ AND
                tmismd.ksai_sha_code = tfib.ksai_sha_code
                AND    to_char(tmismd.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘ --遷移元画面.売上年月
                )
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        AND    tmismd.del_flg = ‘0‘
                                        GROUP  BY tfib.ksai_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT tmih.uriage_sha_code AS ksaiShaCode,
                                            tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic         tfib, --料金情報(基本)
                                               tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                               tb_shukei_group           tsg --集計グループ
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tfib.ksai_sha_code IS NULL
                                        AND    ((EXISTS (SELECT 1
                           FROM   tb_fee_info_basic         t,
                                  tb_moko_info_sub_m_detail t1
                           WHERE  t.receipt_num = tmih.receipt_num
                           AND    t.his = tmih.his
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.mihonshi_sign = ‘1‘
                           AND    t1.ksai_sha_code IN (‘1‘, ‘2‘, ‘3‘, ‘4‘)
                           AND    t.yuko_sign = ‘2‘
                           AND    t.del_flg = ‘0‘
                           AND    t1.del_flg = ‘0‘
                           AND    t.ksai_sha_code = tmih.uriage_sha_code)))
                                        AND    EXISTS (SELECT 1
                    FROM   tb_fee_info_basic         t,
                           tb_moko_info_sub_m_detail t1
                    WHERE  t.receipt_num = tmih.receipt_num
                    AND    t.his = tmih.his
                    AND    t1.receipt_num = tmih.receipt_num
                    AND    t1.his = tmih.his
                    AND    t1.mihonshi_sign = ‘0‘
                    AND    t1.ksai_sha_code = t.ksai_sha_code
                    AND    to_char(t1.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘)
                                       
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        GROUP  BY tmih.uriage_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT ksaiShaCode,
                 shukeiGroupCode,
                 shukeiGroupName,
                 SUM(netFee) AS netFee
          FROM   (
                                         SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                                tsg.shukei_group_name AS shukeiGroupName,
                                                (SELECT MIN(t.ksai_sha_code)
                         FROM   tb_fee_info_basic t
                         WHERE  t.receipt_num = tmih.receipt_num
                         AND    t.his = tmih.his
                         AND    t.yuko_sign = ‘2‘) AS ksaiShaCode,
                                                tfib.net_fee AS netFee
                                         FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                                tb_fee_info_basic         tfib, --料金情報(基本)
                                                tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                                tb_shukei_group           tsg --集計グループ
                                         WHERE  tmih.receipt_num = tfib.receipt_num
                                         AND    tmih.his = tmih.his
                                         AND    tsar.shukei_group_code =
                                                tsg.shukei_group_code
                                         AND    tmih.adv_baitai_bunrui_code =
                                                tsar.adv_baitai_bunrui_code
                                         AND    (tmih.adv_kind_lcode =
                                               tsar.adv_kind_lcode OR
                                               tsar.adv_kind_lcode = ‘*‘)
                                         AND    tfib.ksai_sha_code IS NULL
                 AND    ((NOT EXISTS
                        (SELECT 1
                           FROM   tb_fee_info_basic         t,
                                  tb_moko_info_sub_m_detail t1
                           WHERE  t.receipt_num = tmih.receipt_num
                           AND    t.his = tmih.his
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.del_flg = ‘0‘
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.mihonshi_sign = ‘1‘
                           AND    t1.ksai_sha_code IN (‘1‘, ‘2‘, ‘3‘, ‘4‘)
                           AND    t.yuko_sign = ‘2‘
                           AND    t.del_flg = ‘0‘
                           AND    t1.del_flg = ‘0‘
                           AND    t.ksai_sha_code = tmih.uriage_sha_code)))
                                         AND    EXISTS (SELECT 1
                     FROM   tb_fee_info_basic         t,
                            tb_moko_info_sub_m_detail t1
                     WHERE  t.receipt_num = tmih.receipt_num
                     AND    t.his = tmih.his
                     AND    t1.receipt_num = tmih.receipt_num
                     AND    t1.his = tmih.his
                     AND    t1.mihonshi_sign = ‘0‘
                     AND    t1.ksai_sha_code = t.ksai_sha_code
                     AND    to_char(t1.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘)
                                        
                                         AND    tsar.count_flg = ‘1‘
                                         AND    tsar.input_flg = ‘1‘
                                         AND    tmih.yuko_sign = ‘2‘
                                         AND    tmih.moko_state_code = ‘4‘
                                         AND    tmih.moko_kubun_code <> ‘1‘
                                         AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                         AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                         AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                         AND    tfib.yuko_sign = ‘2‘
                                         AND    tmih.del_flg = ‘0‘
                                         AND    tfib.del_flg = ‘0‘
                                         AND    tsar.del_flg = ‘0‘
                                        
                                         )GROUP  BY ksaiShaCode,
                     shukeiGroupCode,
                     shukeiGroupName
                                   
                                    UNION ALL
                                    --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(英文メディア) の場合
                                    SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                        tsg.shukei_group_code AS shukeiGroupCode,
                                           tsg.shukei_group_name AS shukeiGroupName,
                                           SUM(tfib.net_fee) AS netFee
                                    FROM   tb_moko_info_hanyou      tmih, --申込情報(汎用?基本)
                                           tb_fee_info_basic        tfib, --料金情報(基本)
                                           tb_shukei_adv_relation   tsar, --集計グループ広告媒体関連
                                           tb_shukei_group          tsg, --集計グループ
                                           tb_moko_info_eibun_media tmiem --申込情報(英文メディア)
                                    WHERE  tmih.receipt_num = tfib.receipt_num
                                    AND    tmih.his = tfib.his
                                    AND    tsar.shukei_group_code =
                                           tsg.shukei_group_code
                                    AND    tmih.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    (tmih.adv_kind_lcode =
                                          tsar.adv_kind_lcode OR
                                          tsar.adv_kind_lcode = ‘*‘)
                                    AND    tmih.receipt_num = tmiem.receipt_num
                                    AND    tmih.his = tmiem.his
                                    AND    to_char(tmiem.ksai_date, ‘YYYY/MM‘) =
                                           ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.input_flg = ‘1‘
                                    AND    tmih.yuko_sign = ‘2‘
                                    AND    tmih.moko_state_code = ‘4‘
                                    AND    tmih.moko_kubun_code <> ‘1‘
                                    AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    tfib.yuko_sign = ‘2‘
                                    AND    tmih.del_flg = ‘0‘
                                    AND    tfib.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tmiem.del_flg = ‘0‘
                                    AND    tsg.del_flg=‘0‘
                                   
                                    GROUP  BY tfib.ksai_sha_code,
                                              tsg.shukei_group_code,
                                              tsg.shukei_group_name
                                   
                                    UNION ALL
                                    --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(デジタルメディア) の場合        
                                    SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                        tsg.shukei_group_code AS shukeiGroupCode,
                                           tsg.shukei_group_name AS shukeiGroupName,
                                           SUM(tfib.net_fee) AS netFee
                                    FROM   tb_moko_info_hanyou        tmih, --申込情報(汎用?基本)
                                           tb_fee_info_basic          tfib, --料金情報(基本)
                                           tb_shukei_adv_relation     tsar, --集計グループ広告媒体関連
                                           tb_shukei_group            tsg, --集計グループ
                                           tb_moko_info_digital_media tmidm --申込情報(デジタルメディア)
                                    WHERE  tmih.receipt_num = tfib.receipt_num
                                    AND    tmih.his = tfib.his
                                    AND    tsar.shukei_group_code =
                                           tsg.shukei_group_code
                                    AND    tmih.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    (tmih.adv_kind_lcode =
                                          tsar.adv_kind_lcode OR
                                          tsar.adv_kind_lcode = ‘*‘)
                                    AND    tmih.receipt_num = tmidm.receipt_num
                                    AND    tmih.his = tmidm.his
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.input_flg = ‘1‘
                                    AND    tmih.yuko_sign = ‘2‘
                                    AND    tmih.moko_state_code = ‘4‘
                                    AND    tmih.moko_kubun_code <> ‘1‘
                                    AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    to_char(tmidm.ksai_kikan_start_date, ‘YYYY/MM‘) =
                                           ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tmih.del_flg = ‘0‘
                                    AND    tfib.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tmidm.del_flg = ‘0‘
                                    AND    tsg.del_flg=‘0‘
                                    AND    tfib.yuko_sign = ‘2‘
                                    GROUP  BY tfib.ksai_sha_code,
                                              tsg.shukei_group_code,
                                              tsg.shukei_group_name
                                   
                                    UNION ALL
                                    --申込情報(汎用?基本)[受付番号,履歴番号]→申込情報(事業) の場合
                                    SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                        tsg.shukei_group_code AS shukeiGroupCode,
                                           tsg.shukei_group_name AS shukeiGroupName,
                                           SUM(tfib.net_fee) AS netFee
                                    FROM   tb_moko_info_hanyou    tmih, --申込情報(汎用?基本)
                                           tb_fee_info_basic      tfib, --料金情報(基本)
                                           tb_shukei_adv_relation tsar, --集計グループ広告媒体関連
                                           tb_shukei_group        tsg, --集計グループ
                                           tb_moko_info_jigyo     tmij --申込情報(事業)
                                    WHERE  tmih.receipt_num = tfib.receipt_num
                                    AND    tmih.his = tfib.his
                                    AND    tsar.shukei_group_code =
                                           tsg.shukei_group_code
                                    AND    tmih.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    (tmih.adv_kind_lcode =
                                          tsar.adv_kind_lcode OR
                                          tsar.adv_kind_lcode = ‘*‘)
                                    AND    tmih.receipt_num = tmij.receipt_num
                                    AND    tmih.his = tmij.his
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.input_flg = ‘1‘
                                    AND    tmih.yuko_sign = ‘2‘
                                    AND    tmih.moko_state_code = ‘4‘
                                    AND    tmih.moko_kubun_code <> ‘1‘
                                    AND    to_char(tmij.uriage_date, ‘YYYY/MM‘) =
                                           ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    tfib.yuko_sign = ‘2‘
                                    AND    tmih.del_flg = ‘0‘
                                    AND    tfib.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tmij.del_flg = ‘0‘
                                    AND    tsg.del_flg=‘0‘
                                   
                                    GROUP  BY tfib.ksai_sha_code,
                                              tsg.shukei_group_code,
                                              tsg.shukei_group_name
                                   
                                    )
                            GROUP  BY shukeiGroupCode,
                                      shukeiGroupName,
                                      ksaiShaCode
                           
                            UNION ALL
                            --405e
                            SELECT tmik.ksai_sha_code AS ksaiShaCode,
                                tsg.shukei_group_code AS shukeiGroupCode,
                                   tsg.shukei_group_name AS shukeiGroupName,
                                   SUM(tmik.net_fee) AS netFee
                            FROM   tb_moko_info_keihi     tmik, --経費情報
                                   tb_shukei_adv_relation tsar, --集計グループ広告媒体関連
                                   tb_shukei_group        tsg --集計グループ
                            WHERE  tsar.shukei_group_code = tsg.shukei_group_code
                            AND    tmik.adv_baitai_bunrui_code =
                                   tsar.adv_baitai_bunrui_code
                            AND    tsar.adv_kind_lcode = ‘*‘
                            AND    tsar.count_flg = ‘1‘
                            AND    tsar.input_flg = ‘1‘
                            AND    tmik.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tmik.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tmik.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                            AND    tmik.yuko_sign = ‘2‘
                            AND    tmik.moko_kubun_code <> ‘1‘
                            AND    tmik.keihi_moko_state_code = ‘4‘
                            AND    to_char(tmik.ksai_date, ‘YYYY/MM‘) = ‘2014/06‘ --遷移元画面.売上年月
                            AND    tmik.del_flg = ‘0‘
                            AND    tsar.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tmik.ksai_sha_code,
                                      tsg.shukei_group_code,
                                      tsg.shukei_group_name
                           
                           
                           
                            UNION ALL
                            --405g
                            SELECT tfib.ksai_sha_code AS ksaiShaCode,
                                tsg.shukei_group_code AS shukeiGroupCode,
                                   tsg.shukei_group_name AS shukeiGroupName,
                                   SUM(tfib.net_fee) AS netFee
                            FROM   tb_annai_fee_info_basic  tafib, --案内申込料金情報(基本)
                                   tb_annai_fee_info_detail tafid, --案内申込料金情報(明細)
                                   tb_fee_info_basic        tfib, --料金情報(基本)
                                   tb_annainaka_fee         taf, --案内?案内中料金
                                   tb_gyosyu_sbunrui        tgs, --業種小分類
                                   tb_shukei_adv_relation   tsar, --集計グループ広告媒体関連
                                   tb_shukei_group          tsg --集計グループ
                            WHERE  tafib.annai_fee_mgmt_num =
                                   tafid.annai_fee_mgmt_num
                            AND    tafid.receipt_num = tfib.receipt_num
                            AND    tafid.his = tfib.his
                            AND    tafid.annainaka_fee_num = taf.annainaka_fee_num
                            AND    tafid.annainaka_fee_his = taf.annainaka_fee_his
                            AND    taf.gyosyu_scode = tgs.gyosyu_scode
                            AND    tafib.adv_baitai_bunrui_code =
                                   tsar.adv_baitai_bunrui_code
                            AND    tsar.adv_kind_lcode = ‘*‘
                            AND    tgs.apply_start_date <= tafib.ksai_date
                            AND    tgs.apply_end_date >= tafib.ksai_date
                            AND    tsar.shukei_group_code = tsg.shukei_group_code
                            AND    tsar.count_flg = ‘1‘
                            AND    tsar.input_flg = ‘1‘
                            AND    to_char(tafib.ksai_date, ‘YYYY/MM‘) = ‘2014/06‘ --遷移元画面.売上年月
                            AND    tgs.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                            AND    tafib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                            AND    tafib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                            AND    tafid.yuko_sign = ‘2‘
                            AND    tafid.moko_kubun_code <> ‘1‘
                            AND    tafid.moko_state_code = ‘4‘
                            AND    tfib.yuko_sign = ‘2‘
                            AND    tafib.del_flg = ‘0‘
                            AND    tafid.del_flg = ‘0‘
                            AND    tfib.del_flg = ‘0‘
                            AND    taf.del_flg = ‘0‘
                                  --and tgs.state_code = ‘20‘  --TODO 式样书中未写
                            AND    tsar.del_flg = ‘0‘
                            AND    tsg.del_flg = ‘0‘
                            GROUP  BY tsg.shukei_group_code,
                                      tsg.shukei_group_name,
                                      tfib.ksai_sha_code)
                    GROUP  BY shukeiGroupCode,
                              shukeiGroupName,
                              ksaiShaCode)
            GROUP  BY shukeiGroupCode,
                      shukeiGroupName,
                      ksaiShaCode,
                      netFee)
    GROUP  BY shukeiGroupCode,
              shukeiGroupName))tempa
              inner join TB_Shukei_Group tempb
              on(tempa.shukeiGroupCode=tempb.Shukei_Group_Code)
              order by tempb.Dsp_Jyun
  ]]>
 </select>
 
 <!-- 406 -->
 <!-- 当月売上計(段数) -->
 <select id="selectDansu406" parameterType ="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  with innerView as
  (SELECT /*+materialize*/*
                 FROM   (SELECT tmkii.kumi_item_mgmt_num, --申込組込時項目情報.申込組込時項目管理番号
                                   tsar.shukei_group_code, --集計グループ広告媒体関連.集計グループコード
                                   ROW_NUMBER() OVER(PARTITION BY tmkii.receipt_num, tmkii.ksai_sha_code ORDER BY tmkii.dsp_jyun ASC, tmkii.hansu_code DESC) r
                                    FROM   tb_moko_kumi_item_info tmkii,
                                           tb_shukei_adv_relation tsar
                                    WHERE  1=1
                                    ]]>
                                 AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) IN
                                 <if test="nengetsuList != null and nengetsuList.size() > 0">
                                  <foreach collection="nengetsuList" item="item"
           open="(" separator="," close=")">
           #{item}
          </foreach>
                                 </if>
                                 <![CDATA[
                                    AND    EXISTS
                                     (SELECT 1
                                            FROM   tb_moko_info_basic          tmib,
                                                   tb_moko_info_matching       tmim,
                                                   tb_moko_info_ksai_sha_betsu tmiksb
                                            WHERE  ((tmib.yoyaku_waku_kubun_code IS NOT NULL AND
                                                   tmkii.receipt_num = tmib.yoyaku_waku_receipt_num) OR
                                                   (tmib.yoyaku_waku_kubun_code IS NULL AND
                                                   tmkii.receipt_num = tmib.receipt_num) OR
                              (tmkii.receipt_num = tmim.genko_umeawase_id AND
                              tmim.receipt_num = tmib.receipt_num AND  
                              tmim.his= tmib.his))
                                            AND    tmib.receipt_num = tmiksb.receipt_num
                                            AND    tmib.his = tmiksb.his
                                            AND    tmkii.ksai_sha_code = tmiksb.ksai_sha_code
                                           
                                            AND    tmib.yuko_sign = ‘2‘
                             AND    tmib.moko_state_code = ‘4‘
                             AND    tmib.moko_kubun_code <> ‘1‘
                             AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                             AND    tmib.karihan_sign = ‘0‘
                             AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                             AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                             AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
 
                                            AND    tmib.del_flg = ‘0‘
                                            AND    tmiksb.del_flg = ‘0‘
                                            AND    tmkii.del_flg = ‘0‘
                                            AND    tmib.yuko_sign = ‘2‘
                                            AND    tmkii.jisseki_sign = tmiksb.jisseki_sign)
                                         
                                    AND    (tmkii.han_keiretsu_code NOT IN
                                      ]]>
  <foreach collection="hanKeiretsuCodeList" item="item" open="(" separator="," close=")">
   #{item.codeValue}
        </foreach>
        <![CDATA[
                                        OR tmkii.han_keiretsu_code IS NULL)
                                    AND    tmkii.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                                    AND    (CASE
                                                WHEN EXISTS (SELECT 1
                                                      FROM   tb_shukei_adv_relation tsar
                                                      WHERE  tmkii.adv_kind_lcode = tsar.adv_kind_lcode
                                                      AND    tmkii.adv_baitai_bunrui_code =
                                                             tsar.adv_baitai_bunrui_code
                                                      AND    tsar.count_flg = ‘1‘
                                                      AND    tsar.input_flg = ‘1‘) THEN
                                                 tmkii.adv_kind_lcode
                                                ELSE
                                                 ‘*‘
                                            END) = tsar.adv_kind_lcode
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.dansu_input_flg = ‘1‘)
                 WHERE  r = 1) ,
        innerView2 as
        (SELECT /*+materialize*/*
     FROM   (SELECT tmkii.kumi_item_mgmt_num, --申込組込時項目情報.申込組込時項目管理番号
                    tsar.shukei_group_code, --集計グループ広告媒体関連.集計グループコード
                    ROW_NUMBER() OVER(PARTITION BY tmkii.receipt_num, tmkii.ksai_sha_code  ORDER BY tmkii.ksai_date ASC) r
     
                 FROM   tb_moko_kumi_item_info tmkii,
                        tb_shukei_adv_relation tsar
                 WHERE  1=1
                 ]]>
                                AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) IN
                                <if test="nengetsuList != null and nengetsuList.size() > 0">
                                 <foreach collection="nengetsuList" item="item"
          open="(" separator="," close=")">
          #{item}
         </foreach>
                                </if>
                                <![CDATA[
                 AND    EXISTS
                  (SELECT 1
                         FROM   tb_moko_info_basic          tmib,
                                tb_moko_info_matching       tmim,
                                tb_moko_info_ksai_sha_betsu tmiksb
                         WHERE  ((tmib.yoyaku_waku_kubun_code IS NOT NULL AND
                                tmkii.receipt_num = tmib.yoyaku_waku_receipt_num) OR
                                (tmib.yoyaku_waku_kubun_code IS NULL AND
                                tmkii.receipt_num = tmib.receipt_num) OR
                          (tmkii.receipt_num = tmim.genko_umeawase_id AND
                          tmim.receipt_num = tmib.receipt_num AND  
                          tmim.his= tmib.his))
                         AND    tmib.receipt_num = tmiksb.receipt_num
                         AND    tmib.his = tmiksb.his
                         AND    tmkii.ksai_sha_code = tmiksb.ksai_sha_code
                        
                         AND    tmib.yuko_sign = ‘2‘
                         AND    tmib.moko_state_code = ‘4‘
                         AND    tmib.moko_kubun_code <> ‘1‘
                         AND    tmib.moko_kind = ‘30‘
                         AND    tmib.karihan_sign = ‘0‘
                         AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                         AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                         AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
    
                         AND    tmib.del_flg = ‘0‘
                         AND    tmiksb.del_flg = ‘0‘
                         AND    tmkii.del_flg = ‘0‘
                              
                         AND    tmib.yuko_sign = ‘2‘
                         AND    tmkii.jisseki_sign = tmiksb.jisseki_sign)
                      
                 AND    (tmkii.han_keiretsu_code NOT IN
                                      ]]>
  <foreach collection="hanKeiretsuCodeList" item="item" open="(" separator="," close=")">
         #{item.codeValue}
        </foreach>
        <![CDATA[
                     OR tmkii.han_keiretsu_code IS NULL)
                 AND    tmkii.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                 AND    (CASE
                             WHEN EXISTS (SELECT 1
                                   FROM   tb_shukei_adv_relation tsar
                                   WHERE  tmkii.adv_kind_lcode = tsar.adv_kind_lcode
                                   AND    tmkii.adv_baitai_bunrui_code =
                                          tsar.adv_baitai_bunrui_code
                                   AND    tsar.count_flg = ‘1‘
                                   AND    tsar.input_flg = ‘1‘) THEN
                              tmkii.adv_kind_lcode
                             ELSE
                              ‘*‘
                         END) = tsar.adv_kind_lcode
                 AND    tsar.count_flg = ‘1‘
                 AND    tsar.dansu_input_flg = ‘1‘)
     WHERE  r = 1)
  select * from (SELECT shukeiGroupCode,
         shukeiGroupName,
      
         mikomiDansuT,
         mikomiDansuO,
         mikomiDansuS,
         mikomiDansuN,
         mikomiDansuH
  FROM   (SELECT shukeiGroupCode,
                 shukeiGroupName,
                
                 sum(T) AS mikomiDansuT,
                 sum(O) AS mikomiDansuO,
                 sum(S) AS mikomiDansuS,
                 sum(N) AS mikomiDansuN,
                 sum(H) AS mikomiDansuH
          FROM   (SELECT shukeiGroupCode,
                         shukeiGroupName,
                         CASE
                             WHEN ksaiShaCode = ‘1‘ THEN
                              dansu
                         END AS T,
                         CASE
                             WHEN ksaiShaCode = ‘2‘ THEN
                              dansu
                         END AS O,
                         CASE
                             WHEN ksaiShaCode = ‘3‘ THEN
                              dansu
                         END AS S,
                         CASE
                             WHEN ksaiShaCode = ‘4‘ THEN
                              dansu
                         END AS N,
                         CASE
                             WHEN ksaiShaCode = ‘5‘ THEN
                              dansu
                         END AS H
                  FROM   (SELECT shukeiGroupCode,
                                 shukeiGroupName,
                                 ksaiShaCode,
                                 SUM(dansu) AS dansu
                          FROM   (
                                  --406a
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                          tsg.shukei_group_name AS shukeiGroupName,
                                          ttdk.ksai_sha_code AS ksaiShaCode,
                                          -- SUM(ttdk.tokei_dansu) AS dansu
                                          ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic     tmib,
                                          tb_tokei_dansu_kanri   ttdk, --統計段数管理
                                          tb_moko_kumi_item_info tmkii,
                                          tb_shukei_group        tsg,
                                          innerView
                                  WHERE  tmkii.kumi_item_mgmt_num =
                                         innerView.kumi_item_mgmt_num
                                  AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                                  AND    ((tmib.yoyaku_waku_kubun_code IN (‘1‘,‘3‘,‘5‘) AND
                                        tmkii.receipt_num =
                                        tmib.yoyaku_waku_receipt_num) OR
                                        (tmib.yoyaku_waku_kubun_code IS NULL AND
                                        tmkii.receipt_num = tmib.receipt_num))
                                  AND    tmib.receipt_num = ttdk.receipt_num
                                  AND    tmib.his = ttdk.his
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tmkii.ksai_sha_code = ttdk.ksai_sha_code
                                  AND    innerView.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tmkii.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                  GROUP  BY ttdk.ksai_sha_code,
                                             tsg.shukei_group_code,
                                             tsg.shukei_group_name
                                            
                                  UNION ALL
                                 
                                  --406a--原稿MATCHING
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                          tsg.shukei_group_name AS shukeiGroupName,
                                          ttdk.ksai_sha_code AS ksaiShaCode,
                                          -- SUM(ttdk.tokei_dansu) AS dansu
                                          ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic     tmib,
                                          tb_tokei_dansu_kanri   ttdk, --統計段数管理
                                          tb_moko_kumi_item_info tmkii,
                                          tb_shukei_group        tsg,
                                          innerView
                                  WHERE  tmkii.kumi_item_mgmt_num =
                                         innerView.kumi_item_mgmt_num
                                  AND    tmib.receipt_num = ttdk.receipt_num
                                  AND    tmib.his = ttdk.his
                                  AND    tmkii.ksai_sha_code = ttdk.ksai_sha_code
                                  AND    innerView.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    EXISTS
                                               (SELECT 1
                                                 FROM   tb_moko_info_matching tmim
                                                 WHERE  tmkii.receipt_num =
                                                        tmim.genko_umeawase_id
                                                 AND    tmim.receipt_num =
                                                        tmib.receipt_num
                                                 AND  tmim.his= tmib.his)
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    tmib.moko_kind NOT IN (‘12‘,‘19‘, ‘30‘)
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tmkii.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                  GROUP  BY ttdk.ksai_sha_code,
                                             tsg.shukei_group_code,
                                             tsg.shukei_group_name
                                  UNION ALL
                                 
                                  --406b
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         ttdk.ksai_sha_code AS ksaiShaCode,
                                         --SUM(ttdk.tokei_dansu) AS dansu
                                         ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic     tmib,
                                         tb_tokei_dansu_kanri   ttdk, --統計段数管理
                                         tb_moko_kumi_item_info tmkii,
                                         tb_shukei_group        tsg,
                                         innerView2
                                 
                                  WHERE  tmkii.kumi_item_mgmt_num =
                                         innerView2.kumi_item_mgmt_num
                                  AND    tmkii.ksai_sha_code = ttdk.ksai_sha_code
                                  AND    innerView2.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmib.receipt_num = ttdk.receipt_num
                                  AND    tmib.his = ttdk.his
                                  AND    ((tmib.yoyaku_waku_kubun_code IN (‘1‘,‘3‘,‘5‘) AND
                                        tmkii.receipt_num =
                                        tmib.yoyaku_waku_receipt_num) OR
                                        (tmib.yoyaku_waku_kubun_code IS NULL AND
                                        tmkii.receipt_num = tmib.receipt_num))
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    tmib.moko_kind = ‘30‘
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tmkii.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                  GROUP  BY ttdk.ksai_sha_code,
                                            tsg.shukei_group_code,
                                            tsg.shukei_group_name
                                 
                                  UNION ALL
                                 
                                  --406b--原稿MATCHING
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         ttdk.ksai_sha_code AS ksaiShaCode,
                                         --SUM(ttdk.tokei_dansu) AS dansu
                                         ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic     tmib,
                                         tb_tokei_dansu_kanri   ttdk, --統計段数管理
                                         tb_moko_kumi_item_info tmkii,
                                         tb_shukei_group        tsg,
                                         innerView2
                                 
                                  WHERE  tmkii.kumi_item_mgmt_num =
                                         innerView2.kumi_item_mgmt_num
                                  AND    tmkii.ksai_sha_code = ttdk.ksai_sha_code
                                  AND    innerView2.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmib.receipt_num = ttdk.receipt_num
                                  AND    tmib.his = ttdk.his
                                  AND    EXISTS
                                               (SELECT 1
                                                 FROM   tb_moko_info_matching tmim
                                                 WHERE  tmkii.receipt_num =
                                                        tmim.genko_umeawase_id
                                                 AND    tmim.receipt_num =
                                                        tmib.receipt_num
                                                 AND  tmim.his= tmib.his)
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    tmib.moko_kind = ‘30‘
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    to_char(tmkii.ksai_date, ‘YYYY/MM‘) =  ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tmkii.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                  GROUP  BY ttdk.ksai_sha_code,
                                            tsg.shukei_group_code,
                                            tsg.shukei_group_name
                                           
                                  UNION ALL
                                  --406c
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         ttdk.ksai_sha_code AS ksaiShaCode,
                                         --SUM(ttdk.tokei_dansu) AS dansu
                                         ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic          tmib,
                                         tb_moko_info_ksai_sha_betsu tmiksb,
                                         tb_tokei_dansu_kanri        ttdk,
                                         tb_shukei_adv_relation      tsar,
                                         tb_shukei_group             tsg
                                  WHERE  tmib.receipt_num = tmiksb.receipt_num
                                  AND    tmib.his = tmiksb.his
                                  AND    tmiksb.receipt_num = ttdk.receipt_num
                                  AND    tmiksb.his = ttdk.his
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tmiksb.ksai_sha_code = ttdk.ksai_sha_code
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmib.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
                                  AND    (tmib.adv_kind_lcode =
                                        tsar.adv_kind_lcode OR
                                        tsar.adv_kind_lcode = ‘*‘)
                                   AND   ((to_char(tmib.ksai_kibo_start_date_time_t, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘1‘) OR --遷移元画面.売上年月
                  (to_char(tmib.ksai_kibo_start_date_time_o, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘2‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_s, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘3‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_n, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘4‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_h, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘5‘) ) 
                                  AND    tmiksb.jisseki_sign = ‘1‘            
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kind <> ‘12‘
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    tmib.kumikomi_fuyo_sign <> ‘0‘
                                  AND    tmib.yoyaku_waku_kubun_code IS NULL
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    tmiksb.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tsar.dansu_input_flg = ‘1‘
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY ttdk.ksai_sha_code,
                                            tsg.shukei_group_code,
                                            tsg.shukei_group_name
                                 
                                  UNION ALL
                                  --406d
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         tmiksb.ksai_sha_code AS ksaiShaCode,
                                         --SUM(tmiksb.moko_dansu) AS dansu
                                         ROUND(COALESCE(SUM(tmiksb.moko_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_hanyou         tmih,
                                         tb_moko_info_ksai_sha_betsu tmiksb,
                                          tb_tokei_dansu_kanri        ttdk,
                                         tb_shukei_adv_relation      tsar,
                                         tb_shukei_group             tsg
                                  WHERE  tmih.receipt_num = tmiksb.receipt_num
                                  AND    tmih.his = tmiksb.his
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmiksb.receipt_num = ttdk.receipt_num
                                  AND    tmiksb.his = ttdk.his
                                  AND    tmiksb.ksai_sha_code =
                                         ttdk.ksai_sha_code
                                  AND    tmih.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
                                  AND    (tmih.adv_kind_lcode =
                                        tsar.adv_kind_lcode OR
                                        tsar.adv_kind_lcode = ‘*‘)
                                  AND (CASE WHEN EXISTS (select 1 from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.dansu_input_flg = ‘1‘
                                  AND    tmiksb.jisseki_sign = ‘1‘
                                  AND    to_char(tmiksb.ksai_kibo_start_date_time, ‘YYYY/MM‘) =
                                          ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmih.yuko_sign = ‘2‘
                                  AND    tmih.moko_state_code = ‘4‘
                                  AND    tmih.moko_kubun_code <> ‘1‘
                                  AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmih.del_flg = ‘0‘
                                  AND    tmiksb.del_flg = ‘0‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY tsg.shukei_group_code,
                                            tsg.shukei_group_name,
                                            tmiksb.ksai_sha_code
                                 
                                 
                                 
                                  UNION ALL
                                  --406g
                                  SELECT  tempg.shukeiGroupCode,
                                    tempg.shukeiGroupName,
                                    tempg.ksaiShaCode,
                                    ROUND(COALESCE(SUM(tempg.dansu),0)/100, 2) AS dansu
                                  
                            FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                           tsg.shukei_group_name AS shukeiGroupName,
                                           tafib.ksai_sha_code AS ksaiShaCode,
                                           tafib.Annai_Fee_Mgmt_Num AS annaiFeeMgmtNum,
                                           MAX(tafib.jitsu_dansu) AS dansu
                                           --ROUND(COALESCE(SUM(tafib.jitsu_dansu),0)/100, 2) AS dansu
                                    FROM   tb_annai_fee_info_basic  tafib,
                                           tb_annai_fee_info_detail tafid,
                                           tb_annainaka_fee         taf,
                                           tb_gyosyu_sbunrui        tgs,
                                           tb_shukei_adv_relation   tsar,
                                           tb_shukei_group          tsg
                                    WHERE  tafib.annai_fee_mgmt_num =tafid.annai_fee_mgmt_num
                                    AND    tafid.annainaka_fee_num = taf.annainaka_fee_num
                                    AND    tafid.annainaka_fee_his =taf.annainaka_fee_his
                                    AND    taf.gyosyu_scode = tgs.gyosyu_scode
                                    AND    tgs.apply_start_date <= tafib.ksai_date
                                    AND    tgs.apply_end_date >= tafib.ksai_date
                                    AND    tsg.shukei_group_code= tsar.shukei_group_code
                                    AND    tafib.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    tsar.adv_kind_lcode = ‘*‘
                                    AND    tafib.jisseki_sign = ‘1‘
                                    AND    tafid.Yuko_Sign = ‘2‘
                                    AND    tafib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tafib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.dansu_input_flg = ‘1‘
                                    AND    to_char(tafib.ksai_date, ‘YYYY/MM‘) =
                                            ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tgs.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    tafib.del_flg = ‘0‘
                                    AND    tafid.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tsg.del_flg = ‘0‘
                                   
                                    GROUP  BY tsg.shukei_group_code,
                                              tsg.shukei_group_name,
                                              tafib.ksai_sha_code,
                                              tafib.Annai_Fee_Mgmt_Num) tempg
                            GROUP  BY tempg.shukeiGroupCode,
                                      tempg.shukeiGroupName,
                                      tempg.ksaiShaCode)
                          GROUP  BY shukeiGroupCode,
                                    shukeiGroupName,
                                    ksaiShaCode)
                  GROUP  BY shukeiGroupCode,
                            shukeiGroupName,
                            dansu,
                            ksaiShaCode)
          GROUP  BY shukeiGroupCode,
                    shukeiGroupName))tempa
              inner join TB_Shukei_Group tempb
              on(tempa.shukeiGroupCode=tempb.Shukei_Group_Code)
              order by tempb.Dsp_Jyun
  ]]>
 </select>
 
 <!-- 504 -->
 <!-- 過去月修正 -->
 <select id="selectFee504" parameterType ="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  select * from (SELECT shukeiGroupCode,
         shukeiGroupName,
         CASE
             WHEN (mikomiFeeT IS NOT NULL OR mikomiFeeO IS NOT NULL OR
                  mikomiFeeS IS NOT NULL OR mikomiFeeN IS NOT NULL OR
                  mikomiFeeH IS NOT NULL) THEN
        (nvl(mikomiFeeT,0)+nvl(mikomiFeeO,0)+nvl(mikomiFeeS,0)+nvl(mikomiFeeN,0)+nvl(mikomiFeeH,0))
             ELSE
              (mikomiFeeT+mikomiFeeO+mikomiFeeS+mikomiFeeN+mikomiFeeH)
         END AS mikomiFeeSum,
          mikomiFeeT as mikomiFeeT,
          mikomiFeeO as mikomiFeeO,
          mikomiFeeS as mikomiFeeS,
          mikomiFeeN as mikomiFeeN,
          mikomiFeeH as mikomiFeeH
  FROM   (SELECT shukeiGroupCode AS shukeiGroupCode,
                 shukeiGroupName AS shukeiGroupName,
                 sum(T) AS mikomiFeeT,
                 sum(O) AS mikomiFeeO,
                 sum(S) AS mikomiFeeS,
                 sum(N) AS mikomiFeeN,
                 sum(H) AS mikomiFeeH
          FROM   (SELECT shukeiGroupCode,
                         shukeiGroupName,
                         CASE
                             WHEN ksaiShaCode = ‘1‘ THEN
                              netFee
                         END AS T,
                         CASE
                             WHEN ksaiShaCode = ‘2‘ THEN
                              netFee
                         END AS O,
                         CASE
                             WHEN ksaiShaCode = ‘3‘ THEN
                              netFee
                         END AS S,
                         CASE
                             WHEN ksaiShaCode = ‘4‘ THEN
                              netFee
                         END AS N,
                         CASE
                             WHEN ksaiShaCode = ‘5‘ THEN
                              netFee
                         END AS H
                  FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                 tsg.shukei_group_name AS shukeiGroupName,
                                 tafd.ksai_sha_code AS ksaiShaCode,
                                 SUM(tafd.net_fee) AS netFee
                          FROM   tb_adv_fee_detail      tafd, --広告料金明細
                                 tb_adv_fee_detail      tafd2, --広告料金明細
                                 tb_shukei_adv_relation tsar,
                                 tb_shukei_group        tsg
                          WHERE  tafd.teiseimoto_fee_mgmt_num =
                                 tafd2.fee_mgmt_num
                          AND    tafd.teiseimoto_fee_mgmt_eda =
                                 tafd2.fee_mgmt_eda
                          AND    tsar.shukei_group_code = tsg.shukei_group_code
                          AND    tafd.adv_baitai_bunrui_code =
                                 tsar.adv_baitai_bunrui_code
                          AND    (tafd.adv_kind_lcode = tsar.adv_kind_lcode OR
                                 tsar.adv_kind_lcode = ‘*‘)
                          AND    tafd.teiseimoto_fee_mgmt_num IS NOT NULL
                          AND    tafd.teiseimoto_fee_mgmt_eda IS NOT NULL
                          AND    tafd2.seikyu_shori_flg = ‘2‘
                          AND    tafd.uriage_nengetsu = replace(‘2014/06‘,‘/‘,‘‘) --遷移元画面.売上年月
                          AND    tafd.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                          AND    tafd.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                          AND    tsar.count_flg = ‘1‘
                          AND    tsar.input_flg = ‘1‘
                          AND    tafd.del_flg = ‘0‘
                          AND    tafd2.del_flg = ‘0‘
                          AND    tsar.del_flg = ‘0‘
                          AND    tsg.del_flg = ‘0‘
                          GROUP  BY tsg.shukei_group_code,
                                    tsg.shukei_group_name,
                                    tafd.ksai_sha_code)
                  GROUP  BY shukeiGroupCode,
                            shukeiGroupName,
                            netFee,
                            ksaiShaCode)
          GROUP  BY shukeiGroupCode,
                    shukeiGroupName))tempa
              inner join TB_Shukei_Group tempb
              on(tempa.shukeiGroupCode=tempb.Shukei_Group_Code)
              order by tempb.Dsp_Jyun
  ]]>
 </select>
 
 <!-- 605 -->
 <!-- 申し込み済み計(NET売上) -->
 <select id="selectFee605" parameterType ="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  select * from (SELECT shukeiGroupCode,
         shukeiGroupName,
         CASE
             WHEN (mikomiFeeT IS NOT NULL OR mikomiFeeO IS NOT NULL OR
                  mikomiFeeS IS NOT NULL OR mikomiFeeN IS NOT NULL OR
                  mikomiFeeH IS NOT NULL) THEN
          (nvl(mikomiFeeT,0)+nvl(mikomiFeeO,0)+nvl(mikomiFeeS,0)+nvl(mikomiFeeN,0)+nvl(mikomiFeeH,0))
             ELSE
             (mikomiFeeT+mikomiFeeO+mikomiFeeS+mikomiFeeN+mikomiFeeH)
         END AS mikomiFeeSum,
         mikomiFeeT as mikomiFeeT,
          mikomiFeeO as mikomiFeeO,
          mikomiFeeS as mikomiFeeS,
          mikomiFeeN as mikomiFeeN,
          mikomiFeeH as mikomiFeeH
  FROM   (SELECT shukeiGroupCode AS shukeiGroupCode,
                 shukeiGroupName AS shukeiGroupName,
                 sum(T) AS mikomiFeeT,
                 sum(O) AS mikomiFeeO,
                 sum(S) AS mikomiFeeS,
                 sum(N) AS mikomiFeeN,
                 sum(H) AS mikomiFeeH
          FROM   (SELECT shukeiGroupCode,
                         shukeiGroupName,
                         CASE
                             WHEN ksaiShaCode = ‘1‘ THEN
                              netFee
                         END AS T,
                         CASE
                             WHEN ksaiShaCode = ‘2‘ THEN
                              netFee
                         END AS O,
                         CASE
                             WHEN ksaiShaCode = ‘3‘ THEN
                              netFee
                         END AS S,
                         CASE
                             WHEN ksaiShaCode = ‘4‘ THEN
                              netFee
                         END AS N,
                         CASE
                             WHEN ksaiShaCode = ‘5‘ THEN
                              netFee
                         END AS H
                  FROM   (SELECT shukeiGroupCode,
                                 shukeiGroupName,
                                 ksaiShaCode,
                                 SUM(netFee) AS netFee
                          FROM   (
                                  --605a/605d
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                          tsg.shukei_group_name AS shukeiGroupName,
                                          tfib.ksai_sha_code AS ksaiShaCode,
                                          SUM(tfib.net_fee) AS netFee
                                  FROM   tb_moko_info_basic          tmib,
                                          tb_moko_info_ksai_sha_betsu tmiksb,
                                          tb_fee_info_basic           tfib,
                                          tb_shukei_adv_relation      tsar,
                                          tb_shukei_group             tsg
                                  WHERE  tmib.receipt_num = tmiksb.receipt_num
                                  AND    tmib.his = tmiksb.his
                                  AND    tmiksb.receipt_num = tfib.receipt_num
                                  AND    tmiksb.his = tfib.his
                                  AND    tmiksb.ksai_sha_code = tfib.ksai_sha_code
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmib.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
           AND (CASE WHEN EXISTS
                (select *
                   from tb_shukei_adv_relation tsar
                  where tmib.adv_kind_lcode = tsar.adv_kind_lcode AND tmib.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                    AND tsar.count_flg = ‘1‘
                    AND tsar.input_flg = ‘1‘) THEN
                tmib.adv_kind_lcode ELSE ‘*‘ END) = tsar.adv_kind_lcode
                                   AND   ((to_char(tmib.ksai_kibo_start_date_time_t, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘1‘) OR --遷移元画面.売上年月
                  (to_char(tmib.ksai_kibo_start_date_time_o, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘2‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_s, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘3‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_n, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘4‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_h, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘5‘) )
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    ((tmib.moko_kind <> ‘12‘ AND
                                        (tmib.kumikomi_fuyo_sign = ‘0‘ OR
                                        (tmib.kumikomi_fuyo_sign <> ‘0‘ AND
                                        tmib.yoyaku_waku_kubun_code IS NOT NULL) OR
                                        (tmib.kumikomi_fuyo_sign <> ‘0‘ AND
                                        tmib.yoyaku_waku_kubun_code IS NULL) )) OR
                                        (tmib.moko_kind = ‘12‘ AND
                                        tfib.free_tsukai_genko_num = ‘00‘ AND tfib.Gessho_Kubun = ‘3‘)) --a & d
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tfib.yuko_sign = ‘2‘
                                  AND    tmib.del_flg = ‘0‘
                                  AND    tmiksb.del_flg = ‘0‘
                                  AND    tfib.del_flg = ‘0‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.input_flg = ‘1‘
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.karihan_sign = ‘0‘
                                  GROUP  BY tsg.shukei_group_code,
                                             tsg.shukei_group_name,
                                             tfib.ksai_sha_code
                                 
                                  UNION ALL
                                  --605b
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(掲載社別) の場合
                                        SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                                tsg.shukei_group_name AS shukeiGroupName,
                                                tfib.ksai_sha_code AS ksaiShaCode,
                                                SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou         tmih, --申込情報(汎用?基本)
                                                tb_fee_info_basic           tfib, --料金情報(基本)
                                                tb_shukei_adv_relation      tsar, --集計グループ広告媒体関連
                                                tb_shukei_group             tsg, --集計グループ
                                                tb_moko_info_ksai_sha_betsu tmiksb --申込情報(掲載社別)
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tfib.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tmih.receipt_num =
                                               tmiksb.receipt_num
                                        AND    tmih.his = tmiksb.his
                                        AND    tmiksb.ksai_sha_code = tfib.ksai_sha_code
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    to_char(tmiksb.ksai_kibo_start_date_time, ‘YYYY/MM‘) =
                                               ‘2014/06‘ --遷移元画面.売上年月
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        AND    tmiksb.del_flg = ‘0‘
                                        GROUP  BY tfib.ksai_sha_code,
                                                   tsg.shukei_group_code,
                                                   tsg.shukei_group_name
                                         
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               tfib.ksai_sha_code AS ksaiShaCode,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic         tfib, --料金情報(基本)
                                               tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                               tb_shukei_group           tsg, --集計グループ
                                               tb_moko_info_sub_m_detail tmismd --申込情報(サブメディア地域?見本紙別)
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tmih.receipt_num =
                                               tmismd.receipt_num
                                        AND    tmih.his = tmismd.his
                                        AND    (tmismd.mihonshi_sign = ‘0‘ AND
                tmismd.ksai_sha_code = tfib.ksai_sha_code
                AND    to_char(tmismd.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘ --遷移元画面.売上年月
                )
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmismd.del_flg = ‘0‘
                                       
                                        GROUP  BY tfib.ksai_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               tmih.uriage_sha_code AS ksaiShaCode,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic         tfib, --料金情報(基本)
                                               tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                               tb_shukei_group           tsg --集計グループ
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tfib.ksai_sha_code IS NULL
                                        AND    ((EXISTS (SELECT 1
                           FROM   tb_fee_info_basic         t,
                                  tb_moko_info_sub_m_detail t1
                           WHERE  t.receipt_num = tmih.receipt_num
                           AND    t.his = tmih.his
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.mihonshi_sign = ‘1‘
                           AND    t1.ksai_sha_code IN (‘1‘, ‘2‘, ‘3‘, ‘4‘)
                           AND    t.yuko_sign = ‘2‘
                           AND    t.del_flg = ‘0‘
                           AND    t1.del_flg = ‘0‘
                           AND    t.ksai_sha_code = tmih.uriage_sha_code)))
                                        AND    EXISTS (SELECT 1
                    FROM   tb_fee_info_basic         t,
                           tb_moko_info_sub_m_detail t1
                    WHERE  t.receipt_num = tmih.receipt_num
                    AND    t.his = tmih.his
                    AND    t1.receipt_num = tmih.receipt_num
                    AND    t1.his = tmih.his
                    AND    t1.mihonshi_sign = ‘0‘
                    AND    t1.ksai_sha_code = t.ksai_sha_code
                    AND    to_char(t1.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘)
                                       
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        GROUP  BY tmih.uriage_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(サブメディア) の場合
                                        SELECT shukeiGroupCode,
                 shukeiGroupName,
                 ksaiShaCode,
                 SUM(netFee) AS netFee
          FROM   (
                                         SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                                tsg.shukei_group_name AS shukeiGroupName,
                                                (SELECT MIN(t.ksai_sha_code)
                         FROM   tb_fee_info_basic t
                         WHERE  t.receipt_num = tmih.receipt_num
                         AND    t.his = tmih.his
                         AND    t.yuko_sign = ‘2‘) AS ksaiShaCode,
                                                tfib.net_fee AS netFee
                                         FROM   tb_moko_info_hanyou       tmih, --申込情報(汎用?基本)
                                                tb_fee_info_basic         tfib, --料金情報(基本)
                                                tb_shukei_adv_relation    tsar, --集計グループ広告媒体関連
                                                tb_shukei_group           tsg --集計グループ
                                         WHERE  tmih.receipt_num = tfib.receipt_num
                                         AND    tmih.his = tmih.his
                                         AND    tsar.shukei_group_code =
                                                tsg.shukei_group_code
                                         AND    tmih.adv_baitai_bunrui_code =
                                                tsar.adv_baitai_bunrui_code
                                         AND    (tmih.adv_kind_lcode =
                                               tsar.adv_kind_lcode OR
                                               tsar.adv_kind_lcode = ‘*‘)
                                         AND    tfib.ksai_sha_code IS NULL
                 AND    ((NOT EXISTS
                        (SELECT 1
                           FROM   tb_fee_info_basic         t,
                                  tb_moko_info_sub_m_detail t1
                           WHERE  t.receipt_num = tmih.receipt_num
                           AND    t.his = tmih.his
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.del_flg = ‘0‘
                           AND    t1.receipt_num = tmih.receipt_num
                           AND    t1.his = tmih.his
                           AND    t1.mihonshi_sign = ‘1‘
                           AND    t1.ksai_sha_code IN (‘1‘, ‘2‘, ‘3‘, ‘4‘)
                           AND    t.yuko_sign = ‘2‘
                           AND    t.del_flg = ‘0‘
                           AND    t1.del_flg = ‘0‘
                           AND    t.ksai_sha_code = tmih.uriage_sha_code)))
                                         AND    EXISTS (SELECT 1
                     FROM   tb_fee_info_basic         t,
                            tb_moko_info_sub_m_detail t1
                     WHERE  t.receipt_num = tmih.receipt_num
                     AND    t.his = tmih.his
                     AND    t1.receipt_num = tmih.receipt_num
                     AND    t1.his = tmih.his
                     AND    t1.mihonshi_sign = ‘0‘
                     AND    t1.ksai_sha_code = t.ksai_sha_code
                     AND    to_char(t1.hakou_date, ‘YYYY/MM‘) = ‘2014/06‘)
                                        
                                         AND    tsar.count_flg = ‘1‘
                                         AND    tsar.input_flg = ‘1‘
                                         AND    tmih.yuko_sign = ‘2‘
                                         AND    tmih.moko_state_code = ‘4‘
                                         AND    tmih.moko_kubun_code <> ‘1‘
                                         AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                         AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                         AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                         AND    tfib.yuko_sign = ‘2‘
                                         AND    tmih.del_flg = ‘0‘
                                         AND    tfib.del_flg = ‘0‘
                                         AND    tsar.del_flg = ‘0‘
                                        
                                         )GROUP  BY ksaiShaCode,
                     shukeiGroupCode,
                     shukeiGroupName
                                        
                                         UNION ALL
                                         --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(英文メディア) の場合
                                         SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                                tsg.shukei_group_name AS shukeiGroupName,
                                                tfib.ksai_sha_code AS ksaiShaCode,
                                                SUM(tfib.net_fee) AS netFee
                                         FROM   tb_moko_info_hanyou      tmih, --申込情報(汎用?基本)
                                                tb_fee_info_basic        tfib, --料金情報(基本)
                                                tb_shukei_adv_relation   tsar, --集計グループ広告媒体関連
                                                tb_shukei_group          tsg, --集計グループ
                                                tb_moko_info_eibun_media tmiem --申込情報(英文メディア)
                                         WHERE  tmih.receipt_num = tfib.receipt_num
                                         AND    tmih.his = tmih.his
                                         AND    tsar.shukei_group_code =
                                                tsg.shukei_group_code
                                         AND    tmih.adv_baitai_bunrui_code =
                                                tsar.adv_baitai_bunrui_code
                                         AND    (tmih.adv_kind_lcode =
                                               tsar.adv_kind_lcode OR
                                               tsar.adv_kind_lcode = ‘*‘)
                                         AND    tmih.receipt_num =
                                                tmiem.receipt_num
                                         AND    tmih.his = tmiem.his
                                         AND    to_char(tmiem.ksai_date, ‘YYYY/MM‘) =
                                                ‘2014/06‘ --遷移元画面.売上年月
                                         AND    tsar.count_flg = ‘1‘
                                         AND    tsar.input_flg = ‘1‘
                                         AND    tmih.yuko_sign = ‘2‘
                                         AND    tmih.moko_state_code = ‘4‘
                                         AND    tmih.moko_kubun_code <> ‘1‘
                                         AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                         AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                         AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                         AND    tfib.yuko_sign = ‘2‘
                                         AND    tmih.del_flg = ‘0‘
                                         AND    tfib.del_flg = ‘0‘
                                         AND    tsar.del_flg = ‘0‘
                                         AND    tmiem.del_flg = ‘0‘
                                         GROUP  BY tfib.ksai_sha_code,
                                                   tsg.shukei_group_code,
                                                   tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号] IN 申込情報(デジタルメディア) の場合        
                                        SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               tfib.ksai_sha_code AS ksaiShaCode,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou        tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic          tfib, --料金情報(基本)
                                               tb_shukei_adv_relation     tsar, --集計グループ広告媒体関連
                                               tb_shukei_group            tsg, --集計グループ
                                               tb_moko_info_digital_media tmidm --申込情報(デジタルメディア)
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tmih.receipt_num =
                                               tmidm.receipt_num
                                        AND    tmih.his = tmidm.his
                                        AND    to_char(tmidm.ksai_kikan_start_date, ‘YYYY/MM‘) =
                                               ‘2014/06‘ --遷移元画面.売上年月
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        AND    tmidm.del_flg = ‘0‘
                                        GROUP  BY tfib.ksai_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                       
                                        UNION ALL
                                        --申込情報(汎用?基本)[受付番号,履歴番号]→申込情報(事業) の場合
                                        SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                               tsg.shukei_group_name AS shukeiGroupName,
                                               tfib.ksai_sha_code AS ksaiShaCode,
                                               SUM(tfib.net_fee) AS netFee
                                        FROM   tb_moko_info_hanyou    tmih, --申込情報(汎用?基本)
                                               tb_fee_info_basic      tfib, --料金情報(基本)
                                               tb_shukei_adv_relation tsar, --集計グループ広告媒体関連
                                               tb_shukei_group        tsg, --集計グループ
                                               tb_moko_info_jigyo     tmij --申込情報(事業)
                                        WHERE  tmih.receipt_num = tfib.receipt_num
                                        AND    tmih.his = tmih.his
                                        AND    tsar.shukei_group_code =
                                               tsg.shukei_group_code
                                        AND    tmih.adv_baitai_bunrui_code =
                                               tsar.adv_baitai_bunrui_code
                                        AND    (tmih.adv_kind_lcode =
                                              tsar.adv_kind_lcode OR
                                              tsar.adv_kind_lcode = ‘*‘)
                                        AND    tmih.receipt_num = tmij.receipt_num
                                        AND    tmih.his = tmij.his
                                        AND    to_char(tmij.uriage_date, ‘YYYY/MM‘) =
                                               ‘2014/06‘ --遷移元画面.売上年月
                                        AND    tsar.count_flg = ‘1‘
                                        AND    tsar.input_flg = ‘1‘
                                        AND    tmih.yuko_sign = ‘2‘
                                        AND    tmih.moko_state_code = ‘4‘
                                        AND    tmih.moko_kubun_code <> ‘1‘
                                        AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                        AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                        AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                        AND    tfib.yuko_sign = ‘2‘
                                        AND    tmih.del_flg = ‘0‘
                                        AND    tfib.del_flg = ‘0‘
                                        AND    tsar.del_flg = ‘0‘
                                        AND    tmij.del_flg = ‘0‘
                                        GROUP  BY tfib.ksai_sha_code,
                                                  tsg.shukei_group_code,
                                                  tsg.shukei_group_name
                                 
                                  UNION ALL
                                  --605c
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         tmik.ksai_sha_code AS ksaiShaCode,
                                         SUM(tmik.net_fee) AS netFee
                                  FROM   tb_moko_info_keihi     tmik, --経費情報
                                         tb_shukei_adv_relation tsar, --集計グループ広告媒体関連
                                         tb_shukei_group        tsg --集計グループ
                                  WHERE  tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmik.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
                                  AND    tsar.adv_kind_lcode = ‘*‘
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.input_flg = ‘1‘
                                  AND    tmik.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmik.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmik.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmik.yuko_sign = ‘2‘
                                  AND    tmik.moko_kubun_code <> ‘1‘
                                  AND    tmik.keihi_moko_state_code = ‘4‘
                                  AND    to_char(tmik.ksai_date, ‘YYYY/MM‘) =
                                         ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmik.del_flg = ‘0‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY tmik.ksai_sha_code,
                                            tsg.shukei_group_code,
                                            tsg.shukei_group_name
                                 
                                  UNION ALL
                                  --605e
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         tfib.ksai_sha_code AS ksaiShaCode,
                                         SUM(tfib.net_fee) AS netFee
                                  FROM   tb_annai_fee_info_basic  tafib, --案内申込料金情報(基本)
                                         tb_annai_fee_info_detail tafid, --案内申込料金情報(明細)
                                         tb_fee_info_basic        tfib, --料金情報(基本)
                                         tb_annainaka_fee         taf, --案内?案内中料金
                                         tb_gyosyu_sbunrui        tgs, --業種小分類
                                         tb_shukei_adv_relation   tsar, --集計グループ広告媒体関連
                                         tb_shukei_group          tsg --集計グループ
                                  WHERE  tafib.annai_fee_mgmt_num =
                                         tafid.annai_fee_mgmt_num
                                  AND    tafid.receipt_num = tfib.receipt_num
                                  AND    tafid.his = tfib.his
                                  AND    tafid.annainaka_fee_num =
                                         taf.annainaka_fee_num
                                  AND    tafid.annainaka_fee_his =
                                         taf.annainaka_fee_his
                                  AND    taf.gyosyu_scode = tgs.gyosyu_scode
                                  AND    tgs.apply_start_date <= tafib.ksai_date
                                  AND    tgs.apply_end_date >= tafib.ksai_date
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tafib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tafib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tafib.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
                                  AND    tsar.adv_kind_lcode = ‘*‘
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.input_flg = ‘1‘
                                  AND    to_char(tafib.ksai_date, ‘YYYY/MM‘) = ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tgs.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tafid.yuko_sign = ‘2‘
                                  AND    tafid.moko_kubun_code <> ‘1‘
                                  AND    tafid.moko_state_code = ‘4‘
                                  AND    tfib.yuko_sign = ‘2‘
                                  AND    tafib.del_flg = ‘0‘
                                  AND    tafid.del_flg = ‘0‘
                                  AND    tfib.del_flg = ‘0‘
                                  AND    taf.del_flg = ‘0‘
                                        --and tgs.state_code = ‘20‘  --TODO 式样书中未写
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY tsg.shukei_group_code,
                                            tsg.shukei_group_name,
                                            tfib.ksai_sha_code)
                          GROUP  BY shukeiGroupCode,
                                    shukeiGroupName,
                                    ksaiShaCode)
                  GROUP  BY shukeiGroupCode,
                            shukeiGroupName,
                            ksaiShaCode,
                            netFee)
          GROUP  BY shukeiGroupCode,
                    shukeiGroupName))tempa
              inner join TB_Shukei_Group tempb
              on(tempa.shukeiGroupCode=tempb.Shukei_Group_Code)
              order by tempb.Dsp_Jyun
  ]]>
 </select>
 
 <!-- 606 -->
 <!-- 申し込み済み計(段数) -->
 <select id="selectDansu606" parameterType ="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiSearchDto"
  resultType="jp.co.asahi_np.biz.bbs.pcs.dto.mik.ShukoMikomiDto">
  <![CDATA[
  select * from (
SELECT shukeiGroupCode,
         shukeiGroupName,
       
         mikomiDansuT,
         mikomiDansuO,
         mikomiDansuS,
         mikomiDansuN,
         mikomiDansuH
  FROM   (SELECT shukeiGroupCode,
                 shukeiGroupName,
                
                 sum(T) AS mikomiDansuT,
                 sum(O) AS mikomiDansuO,
                 sum(S) AS mikomiDansuS,
                 sum(N) AS mikomiDansuN,
                 sum(H) AS mikomiDansuH
          FROM   (SELECT shukeiGroupCode,
                         shukeiGroupName,
                         CASE
                             WHEN ksaiShaCode = ‘1‘ THEN
                              dansu
                         END AS T,
                         CASE
                             WHEN ksaiShaCode = ‘2‘ THEN
                              dansu
                         END AS O,
                         CASE
                             WHEN ksaiShaCode = ‘3‘ THEN
                              dansu
                         END AS S,
                         CASE
                             WHEN ksaiShaCode = ‘4‘ THEN
                              dansu
                         END AS N,
                         CASE
                             WHEN ksaiShaCode = ‘5‘ THEN
                              dansu
                         END AS H
                  FROM   (SELECT shukeiGroupCode,
                                 shukeiGroupName,
                                 ksaiShaCode,
                                 SUM(dansu) AS dansu
                          FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         ttdk.ksai_sha_code AS ksaiShaCode,
                                         --SUM(ttdk.tokei_dansu) AS dansu
                                         ROUND(COALESCE(SUM(ttdk.tokei_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_basic          tmib,
                                         tb_moko_info_ksai_sha_betsu tmiksb,
                                         tb_tokei_dansu_kanri        ttdk,
                                         tb_shukei_adv_relation      tsar,
                                         tb_shukei_group             tsg
                                  WHERE  tmib.receipt_num = tmiksb.receipt_num
                                  AND    tmib.his = tmiksb.his
                                  AND    tmiksb.receipt_num = ttdk.receipt_num
                                  AND    tmiksb.his = ttdk.his
                                  AND    tmiksb.ksai_sha_code =
                                         ttdk.ksai_sha_code
                                  AND (CASE WHEN EXISTS (select * from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                  AND    tmib.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
           AND (CASE WHEN EXISTS
                (select *
                   from tb_shukei_adv_relation tsar
                  where tmib.adv_kind_lcode = tsar.adv_kind_lcode AND tmib.adv_baitai_bunrui_code = tsar.adv_baitai_bunrui_code
                    AND tsar.count_flg = ‘1‘
                    AND tsar.input_flg = ‘1‘) THEN
                tmib.adv_kind_lcode ELSE ‘*‘ END) = tsar.adv_kind_lcode
                                  AND   ((to_char(tmib.ksai_kibo_start_date_time_t, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘1‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_o, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘2‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_s, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘3‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_n, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘4‘) OR
                  (to_char(tmib.ksai_kibo_start_date_time_h, ‘YYYY/MM‘) =  ‘2014/06‘ AND  tmiksb.ksai_sha_code =‘5‘) )
                                  AND    tmiksb.jisseki_sign = ‘1‘
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.dansu_input_flg = ‘1‘
                                  AND    tmib.yuko_sign = ‘2‘
                                  AND    tmib.moko_state_code = ‘4‘
                                  AND    tmib.moko_kubun_code <> ‘1‘
                                  AND    tmib.moko_kind <> ‘12‘
                                  AND    tmib.karihan_sign = ‘0‘
                                  AND    (tmib.kumikomi_fuyo_sign = ‘0‘ OR
                                        (tmib.kumikomi_fuyo_sign <> ‘0‘ AND
                                        tmib.yoyaku_waku_kubun_code IS NOT NULL) OR
                                         (tmib.kumikomi_fuyo_sign <> ‘0‘ AND
                                        tmib.yoyaku_waku_kubun_code IS NULL))
                                  AND    tmib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmib.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmib.del_flg = ‘0‘
                                  AND    tmiksb.del_flg = ‘0‘
                                  AND    ttdk.del_flg = ‘0‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY tsg.shukei_group_code,
                                            tsg.shukei_group_name,
                                            ttdk.ksai_sha_code
                                 
                                  UNION ALL
                                  --606b
                                  SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                         tsg.shukei_group_name AS shukeiGroupName,
                                         tmiksb.ksai_sha_code AS ksaiShaCode,
                                         --SUM(tmiksb.moko_dansu) AS dansu
                                         ROUND(COALESCE(SUM(tmiksb.moko_dansu),0)/1000, 3) AS dansu
                                  FROM   tb_moko_info_hanyou         tmih,
                                         tb_moko_info_ksai_sha_betsu tmiksb,
                                            tb_tokei_dansu_kanri        ttdk,
                                         tb_shukei_adv_relation      tsar,
                                         tb_shukei_group             tsg
                                  WHERE  tmih.receipt_num = tmiksb.receipt_num
                                  AND    tmih.his = tmiksb.his
                                  AND    tsar.shukei_group_code =
                                         tsg.shukei_group_code
                                           AND    tmiksb.receipt_num = ttdk.receipt_num
                                  AND    tmiksb.his = ttdk.his
                                  AND    tmiksb.ksai_sha_code =
                                         ttdk.ksai_sha_code
                                  AND (CASE WHEN EXISTS (select * from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND tfib.fee_state_code = ‘40‘)
                                   THEN (select tfib.fee_mgmt_num from TB_FEE_INFO_BASIC tfib where ttdk.receipt_num = tfib.receipt_num AND ttdk.his = tfib.his AND rownum = 1)
                                   ELSE ‘************‘ END) = ttdk.fee_mgmt_num
                                  AND    tsar.count_flg = ‘1‘
                                  AND    tsar.dansu_input_flg = ‘1‘
                                  AND    tmiksb.jisseki_sign = ‘1‘
                                  AND    tmih.adv_baitai_bunrui_code =
                                         tsar.adv_baitai_bunrui_code
                                  AND    (tmih.adv_kind_lcode =
                                        tsar.adv_kind_lcode OR
                                        tsar.adv_kind_lcode = ‘*‘)
                                  AND    to_char(tmiksb.ksai_kibo_start_date_time, ‘YYYY/MM‘) =
                                         ‘2014/06‘ --遷移元画面.売上年月
                                  AND    tmih.yuko_sign = ‘2‘
                                  AND    tmih.moko_state_code = ‘4‘
                                  AND    tmih.moko_kubun_code <> ‘1‘
                                  AND    tmih.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                  AND    tmih.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                  AND    tmih.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                  AND    tmih.del_flg = ‘0‘
                                  AND    tmiksb.del_flg = ‘0‘
                                  AND    tsar.del_flg = ‘0‘
                                  AND    tsg.del_flg = ‘0‘
                                 
                                  GROUP  BY tsg.shukei_group_code,
                                            tsg.shukei_group_name,
                                            tmiksb.ksai_sha_code
                                 
                                
                                 
                                  UNION ALL
                                  --606e
                                   --406g
                                  SELECT  tempg.shukeiGroupCode,
                                    tempg.shukeiGroupName,
                                    tempg.ksaiShaCode,
                                    ROUND(COALESCE(SUM(tempg.dansu),0)/100, 2) AS dansu
                                  
                            FROM   (SELECT tsg.shukei_group_code AS shukeiGroupCode,
                                           tsg.shukei_group_name AS shukeiGroupName,
                                           tafib.ksai_sha_code AS ksaiShaCode,
                                           tafib.Annai_Fee_Mgmt_Num AS annaiFeeMgmtNum,
                                           MAX(tafib.jitsu_dansu) AS dansu
                                           --ROUND(COALESCE(SUM(tafib.jitsu_dansu),0)/100, 2) AS dansu
                                    FROM   tb_annai_fee_info_basic  tafib,
                                           tb_annai_fee_info_detail tafid,
                                           tb_annainaka_fee         taf,
                                           tb_gyosyu_sbunrui        tgs,
                                           tb_shukei_adv_relation   tsar,
                                           tb_shukei_group          tsg
                                    WHERE  tafib.annai_fee_mgmt_num =tafid.annai_fee_mgmt_num
                                    AND    tafid.annainaka_fee_num = taf.annainaka_fee_num
                                    AND    tafid.annainaka_fee_his =taf.annainaka_fee_his
            AND    tafid.Yuko_Sign = ‘2‘
                                    AND    taf.gyosyu_scode = tgs.gyosyu_scode
                                    AND    tgs.apply_start_date <= tafib.ksai_date
                                    AND    tgs.apply_end_date >= tafib.ksai_date
                                    AND    tsg.shukei_group_code= tsar.shukei_group_code
                                    AND    tafib.jisseki_sign = ‘1‘
                                    AND    tafib.uriage_sha_code = ‘1‘ --遷移元画面.売上社コード
                                    AND    tafib.uriage_bu_ka_code = ‘110‘ --遷移元画面.担当部課コード
                                    AND    tafib.adv_baitai_bunrui_code =
                                           tsar.adv_baitai_bunrui_code
                                    AND    tsar.adv_kind_lcode = ‘*‘
                                    AND    tsar.count_flg = ‘1‘
                                    AND    tsar.dansu_input_flg = ‘1‘
                                    AND    to_char(tafib.ksai_date, ‘YYYY/MM‘) =
                                            ‘2014/06‘ --遷移元画面.売上年月
                                    AND    tgs.gyosyu_mcode = ‘10‘ --遷移元画面.業種中分類コード
                                    AND    tafib.del_flg = ‘0‘
                                    AND    tafid.del_flg = ‘0‘
                                    AND    tsar.del_flg = ‘0‘
                                    AND    tsg.del_flg = ‘0‘
                                   
                                    GROUP  BY tsg.shukei_group_code,
                                              tsg.shukei_group_name,
                                              tafib.ksai_sha_code,
                                              tafib.Annai_Fee_Mgmt_Num) tempg
                            GROUP  BY tempg.shukeiGroupCode,
                                      tempg.shukeiGroupName,
                                      tempg.ksaiShaCode)
                          GROUP  BY shukeiGroupCode,
                                    shukeiGroupName,
                                    ksaiShaCode)
                  GROUP  BY shukeiGroupCode,
                            shukeiGroupName,
                            dansu,
                            ksaiShaCode)
          GROUP  BY shukeiGroupCode,
                    shukeiGroupName))tempa
              inner join TB_Shukei_Group tempb
              on(tempa.shukeiGroupCode=tempb.Shukei_Group_Code)
              order by tempb.Dsp_Jyun
  ]]>
 </select>
 
 Parameters: 2014/06(String), 2014/05(String), 2014/07(String), 1(String), 110(String), 10(String), T2(String), T3(String), 2014/06(String), 2014/05(String), 2014/07(String), 1(String), 110(String), 10(String), T2(String), T3(String), 2014/06(String), 1(String), 110(String), 10(String), 2014/06(String), 1(String), 110(String), 10(String), 2014/06(String), 1(String), 110(String), 10(String), 20