<?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

性能改善后复杂SQL的更多相关文章

  1. 待性能改善的一个SQL

    select t.*, t.rowid from tb_tk_datasakusei_ctrl t; alter table ATOMBB.TB_TK_JISSEKI_INFO_DETAIL add ...

  2. sql server 查询性能最差的sql语句

    SELECT TOP 10 TEXT AS 'SQL Statement' ,last_execution_time AS 'Last Execution Time' ,(total_logical_ ...

  3. 从性能的角度谈SQL Server聚集索引键的选择

      简介 在SQL Server中,数据是按页进行存放的.而为表加上聚集索引后,SQL Server对于数据的查找就是按照聚集索引的列作为关键字进行了.因此对于聚集索引的选择对性能的影响就变得十分重要 ...

  4. 阿里数据库性能诊断的利器——SQL执行干预

    概述 在业务数据库性能问题诊断中,如果发现一个业务性能很差跟某个SQL有关,应用连接池几乎被该SQL占满,同时数据库服务器上也不堪重负.此时情况很紧急,业务改SQL重发布已经来不及了,运维能选择的操作 ...

  5. 【SQL Server性能优化】运用SQL Server的全文检索来提高模糊匹配的效率

    原文:[SQL Server性能优化]运用SQL Server的全文检索来提高模糊匹配的效率 今天去面试,这个公司的业务需要模糊查询数据,之前他们通过mongodb来存储数据,但他们说会有丢数据的问题 ...

  6. MySQL性能优化(四):SQL优化

    原文:MySQL性能优化(四):SQL优化 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ ...

  7. 十八般武艺玩转GaussDB(DWS)性能调优:SQL改写

    摘要:本文将系统介绍在GaussDB(DWS)系统中影响性能的坏味道SQL及SQL模式,帮助大家能够从原理层面尽快识别这些坏味道SQL,在调优过程中及时发现问题,进行整改. 数据库的应用中,充斥着坏味 ...

  8. 如何找出你性能最差的SQL Server查询

    我经常会被反复问到这样的问题:”我有一个性能很差的SQL Server.我如何找出最差性能的查询?“.因此在今天的文章里会给你一些让你很容易找到问题答案的信息向导. 问SQL Server! SQL ...

  9. 性能相差极大的SQL语句

    等价的SQL,性能差异极大,数据库里设计了一个字段存储日期时间,但不是datetime类型,用了时间戳(int 11), 下面有2个SQL语句用于查询数据库,一个是把时间戳转成date进行查询,一个是 ...

随机推荐

  1. java-多线程的入门_进阶总结

    多线程 概述图 1.概述 进程:正在执行中的程序,其实时应用程序在内存中运行的那片空间. 线程:进程中的一个执行单元,负责进程中的程序的运行,一个进程中至少要有一个线程. (进程可以理解为是一个QQ程 ...

  2. 【模板】KMP [2017年5月计划 清北学堂51精英班Day2]

    Day2就搞一个KMP把  马拉车.AC自动机等准备省选的时候再说.. 模板题: 1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze     ...

  3. Leetcode96.Unique Binary Search Trees不同的二叉搜索树

    给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 假设n个节点存在二叉排序树的 ...

  4. Lavavel 程序报错 MassAssignmentException in Model.php line 452: _token

    Lavarel 用类似于下面命令插入数据时候出错 Comment::create($request->all()) 错误页面截图如下: 错误原因:下面这行代码应该写到对应的Model里面,而不是 ...

  5. Mac 下终端命令行之基本命令总结(持续更新)

    最近用Mac做一些开发,用到了一些命令行的内容,先将常用的命令行进行总结.由于会不断的用到新的,所以将会持续的总结进来.每一个命令行的使用可能都会比较复杂,我只会总结最常用的使用方法. echo命令 ...

  6. SEO优化步骤

    SEO技术并不是简单的几个建议,而是一项需要足够耐心和细致的脑力劳动.大体上,SEO优化主要分为8小步: 1.关键词分析(也叫关键词定位) 这是进行SEO优化最重要的一环,关键词分析包括:关键词关注量 ...

  7. ASP.NET自定义控件组件开发 第一章 第三篇 第一章的完结篇

    ASP.NET自定义控件组件开发 第一章 第三篇   第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ...

  8. 【linux配置】虚拟机配置静态IP地址

    使用VMware配置虚拟机静态IP地址 一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...

  9. 解决IE6、IE7、Firefox兼容最简单的CSS Hack

    写三句代码来控制一个属性,区别Firefox,IE7,IE6: background:orange; *background:green !important; *background:blue;   ...

  10. 学习JDK1.8集合源码之--HashSet

    1. HashSet简介 HashSet是一个不可重复的无序集合,底层由HashMap实现存储,故HashSet是非线程安全的,由于HashSet使用HashMap的Key来存储元素,而HashMap ...