码迷,mamicode.com
首页 > 其他好文 > 详细

UNION ALL的用法

时间:2019-04-23 10:56:03      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:1.0   dma   code   page   pex   start   off   policy   odi   

<?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="com.j1.soa.resource.order.oracle.dao.TpInfoMapper">
    <resultMap id="TpInfoResultMap" type="TpInfo">
        <result column="ID" jdbcType="DECIMAL" property="id" />
        <result column="AVAILABLE_BALANCE" jdbcType="DECIMAL" property="availableBalance" />
    </resultMap>

    <resultMap id="TpPolicyResultMap" type="TpInfo">
        <result column="END_DATE" jdbcType="VARCHAR" property="dueDate" />
        <result column="BALANCE" jdbcType="VARCHAR" property="dueAmount" />
    </resultMap>

    <resultMap id="TpConsumeRecordMap" type="TpConsumeRecord">
        <result column="POLICY_NO" jdbcType="VARCHAR" property="policyNo" />
        <result column="BUSINESS_ID" jdbcType="VARCHAR" property="businessId" />
        <result column="AMOUNT" jdbcType="VARCHAR" property="amount" />
        <result column="ADD_TIME" jdbcType="VARCHAR" property="amountTime" />
        <result column="OPERATION" jdbcType="VARCHAR" property="amountStatus" />
    </resultMap>

    <resultMap id="AmountSumMap" type="TpConsumeRecord">
        <result column="AMOUNT" jdbcType="VARCHAR" property="amount" />
        <result column="OPERATION" jdbcType="VARCHAR" property="amountStatus" />
    </resultMap>

    <sql id="be_fy">
        select *
        from (select row_.*, rownum rownum_
        from (
    </sql>
    <!-- 用于分页的尾部 -->
    <sql id="ed_fy">
        ) row_
        where 1=1
        <![CDATA[
                 and    rownum <= #{offset}+#{pageSize}
                 ]]>
        )
        where 1=1
        <![CDATA[
                 and    rownum_ > #{offset}
                 ]]>
    </sql>

    <select id="checkIsTp" parameterType="java.lang.Long" resultType="java.lang.Integer">
        select count(*)
        from fec.tp_account tpa
        where tpa.member_id = #{memberId,jdbcType=DECIMAL}
        and tpa.is_delete = N
    </select>

    <select id="getTpInfo" parameterType="java.lang.Long" resultMap="TpInfoResultMap">
        select tpp.tp_account_id as id, sum(tpp.balance) as available_balance
          from fec.tp_account tpa, fec.tp_policy tpp
         where tpa.member_id = #{memberId,jdbcType=DECIMAL}
           and tpa.id = tpp.tp_account_id
           and tpa.is_delete = N
         group by tpp.tp_account_id
    </select>

    <select id="getLimitPolicy" parameterType="java.lang.Long" resultMap="TpPolicyResultMap">
        select tpp.balance,tpp.end_date
        from fec.tp_policy tpp
        where tpp.tp_account_id = #{tpAccountId,jdbcType=DECIMAL}
         <![CDATA[ and TO_DATE(tpp.end_date, ‘yyyy-mm-dd‘) <=
        TO_DATE(TO_CHAR(sysdate + interval ‘2‘ month, ‘yyyy-mm-dd‘),
        ‘yyyy-mm-dd‘)
        and TO_DATE(tpp.start_date, ‘yyyy-mm-dd‘) <= TO_DATE(TO_CHAR(sysdate,‘yyyy-mm-dd‘), ‘yyyy-mm-dd‘)
        and TO_DATE(tpp.end_date, ‘yyyy-mm-dd‘) >=TO_DATE(TO_CHAR(sysdate, ‘yyyy-mm-dd‘), ‘yyyy-mm-dd‘)
        and tpp.is_delete = ‘N‘
        ]]>
        order by tpp.end_date
    </select>

    <select id="getTpConsumeRecordList" parameterType="Pageable" resultMap="TpConsumeRecordMap">
        <include refid="be_fy"/>
        select tpo.policy_no,
               tpo.business_id,
               sum(tpo.amount) || ‘‘ as amount,
               tpo.add_time,
               tpo.OPERATION
        from fec.tp_policy_operation_log tpo,fec.tp_account tpa,fec.tp_policy tpp
        where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{params.memberId}
            <if test="params.beginDate != null">
                <![CDATA[
                 and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) >= TO_DATE(#{params.beginDate},‘yyyy-mm-dd hh24:mi:ss‘)
                ]]>
            </if>
            <if test="params.endDate != null">
                <![CDATA[
                and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) <= TO_DATE(#{params.endDate},‘yyyy-mm-dd hh24:mi:ss‘)
                ]]>
            </if>
         group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
         order by tpo.add_time desc
        <include refid="ed_fy"/>
    </select>

    <select id="getTpConsumeRecordListCount" parameterType="TpConsumeRecord" resultType="Integer">
        select count(1)
          from
            (select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,fec.tp_account tpa,fec.tp_policy tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{memberId}
            <if test="beginDate != null">
                <![CDATA[
                     and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) >= TO_DATE(#{beginDate},‘yyyy-mm-dd hh24:mi:ss‘)
                    ]]>
            </if>
            <if test="endDate != null">
                <![CDATA[
                    and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) <= TO_DATE(#{endDate},‘yyyy-mm-dd hh24:mi:ss‘)
                    ]]>
            </if>
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
            order by tpo.add_time desc)
    </select>

    <select id="getAmountSumByOperation" parameterType="TpConsumeRecord" resultMap="AmountSumMap">
        select sum(tpo.amount) || ‘‘ as amount, tpo.OPERATION
        from fec.tp_policy_operation_log tpo,
        fec.tp_account              tpa,
        fec.tp_policy               tpp
        where tpa.id = tpp.tp_account_id
        and tpa.is_delete = N
        and tpp.policy_no = tpo.policy_no
        and tpp.is_delete = N
        and tpa.member_id = #{memberId}
        <if test="beginDate != null">
            <![CDATA[
                     and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) >= TO_DATE(#{beginDate},‘yyyy-mm-dd hh24:mi:ss‘)
                    ]]>
        </if>
        <if test="endDate != null">
            <![CDATA[
                    and to_date(tpo.add_time,‘yyyy-mm-dd hh24:mi:ss‘) <= TO_DATE(#{endDate},‘yyyy-mm-dd hh24:mi:ss‘)
                    ]]>
        </if>
        group by tpo.OPERATION
    </select>

    <select id="getTpExceptionInfo" parameterType="Pageable" resultMap="TpConsumeRecordMap">
        <include refid="be_fy"/>
        <if test="params.amountStatus != all and params.amountStatus != 99">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{params.memberId}
            and tpo.operation in (${params.amountStatus})
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
        </if>
        <if test="params.amountStatus == all">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{params.memberId}
            and tpo.operation in (1,3)
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
            union all
            select tpp.policy_no,
            invalid as business_id,
            tpp.balance || ‘‘,
            tpp.end_date as add_time,
            99 as OPERATION
            from fec.tp_account tpa, fec.tp_policy tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.is_delete = N
            and tpa.member_id =  #{params.memberId}
        <![CDATA[
            and TO_DATE(tpp.end_date || ‘23:59:59‘, ‘yyyy-mm-dd hh24:mi:ss‘) <
                to_date(to_char(sysdate, ‘yyyy-mm-dd‘) || ‘ 23:59:59‘,
            ‘yyyy-mm-dd hh24:mi:ss‘)
            ]]>
        </if>
        <if test="params.amountStatus == 99">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{params.memberId}
            and tpo.operation in (2)
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
            UNION  ALL
            select tpp.policy_no,
            invalid as business_id,
            tpp.balance || ‘‘,
            tpp.end_date as add_time,
            99 as OPERATION
            from fec.tp_account tpa, fec.tp_policy tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.is_delete = N
            and tpa.member_id =  #{params.memberId}
            <![CDATA[
            and TO_DATE(tpp.end_date || ‘23:59:59‘, ‘yyyy-mm-dd hh24:mi:ss‘) <
                to_date(to_char(sysdate, ‘yyyy-mm-dd‘) || ‘ 23:59:59‘,
            ‘yyyy-mm-dd hh24:mi:ss‘)
            ]]>
        </if>
        order by add_time desc
        <include refid="ed_fy"/>
    </select>

    <select id="getTpExceptionInfoCount" parameterType="TpConsumeRecord" resultType="Integer">
        select count(1)
        from
        (
        <if test="amountStatus != all and amountStatus != 99">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{memberId}
            and tpo.operation in (${amountStatus})
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
        </if>
        <if test="amountStatus == all">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{memberId}
            and tpo.operation in (1,3)
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
            union all
            select tpp.policy_no,
            invalid as business_id,
            tpp.balance || ‘‘,
            tpp.end_date as add_time,
            99 as OPERATION
            from fec.tp_account tpa, fec.tp_policy tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.is_delete = N
            and tpa.member_id =  #{memberId}
            <![CDATA[
            and TO_DATE(tpp.end_date || ‘23:59:59‘, ‘yyyy-mm-dd hh24:mi:ss‘) <
                to_date(to_char(sysdate, ‘yyyy-mm-dd‘) || ‘ 23:59:59‘,
            ‘yyyy-mm-dd hh24:mi:ss‘)
            ]]>
        </if>
        <if test="amountStatus == 99">
            select tpo.policy_no,
            tpo.business_id,
            sum(tpo.amount) || ‘‘ as amount,
            tpo.add_time,
            tpo.OPERATION
            from fec.tp_policy_operation_log tpo,
            fec.tp_account              tpa,
            fec.tp_policy               tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.policy_no = tpo.policy_no
            and tpp.is_delete = N
            and tpa.member_id = #{memberId}
            and tpo.operation in (2)
            group by tpo.policy_no, tpo.business_id, tpo.add_time, tpo.OPERATION
            UNION  ALL
            select tpp.policy_no,
            invalid as business_id,
            tpp.balance || ‘‘,
            tpp.end_date as add_time,
            99 as OPERATION
            from fec.tp_account tpa, fec.tp_policy tpp
            where tpa.id = tpp.tp_account_id
            and tpa.is_delete = N
            and tpp.is_delete = N
            and tpa.member_id =  #{memberId}
            <![CDATA[
            and TO_DATE(tpp.end_date || ‘23:59:59‘, ‘yyyy-mm-dd hh24:mi:ss‘) <
                to_date(to_char(sysdate, ‘yyyy-mm-dd‘) || ‘ 23:59:59‘,
            ‘yyyy-mm-dd hh24:mi:ss‘)
            ]]>
        </if>
        order by add_time desc)
    </select>
</mapper>

 

UNION ALL的用法

标签:1.0   dma   code   page   pex   start   off   policy   odi   

原文地址:https://www.cnblogs.com/wangchuanfu/p/10071191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!