一对多sql

<!--  分页查询派货成本  -->
  <select id="queryCostRegionPriceBycondtion" parameterMap="CostRegionPriceQueryEntity" resultMap="CostRegionPriceResultEntity">
 	SELECT
		b.AUDIT_HOUR,
		b.OPER_STATUS,
		b.RELE_ID,
		b.STAR_REGION_ID,
		b.END_REGION_ID,
		b.START_REGION__NAME,
		b.END_REGION_NAME,
		b.CREATE_USER_CODE||'('||e1.EMPLOYEE_NAME||')' CREATE_USER_CODE,
		b.MODIFY_USER_CODE,
		b.CREATE_TIME,
		b.MODIFY_TIME,
		b.DEL_FLAG,
		b.REMARK,
		b.COST_NAME,
		b.START_REGION_TYPE,
		b.END_REGION_TYPE,
		b.CALC_TYPE,
		b.BIZ_TYPE,
		b.TIME_CODE,
		b.PRODUCT_CODE,
		b.DIS_TYPE,
		b.START_TIME,
		b.END_TIME,
		b.PRICE_ITEM_CODE,
		
		c.audit_time,
		c.audit_user_code||'('||e2.EMPLOYEE_NAME||')' audit_user_code,
		case WHEN  
		c.AUDIT_STATUS = 0 and 
		<![CDATA[SYSDATE > c.START_TIME]]>
		then 2
		WHEN  
		c.AUDIT_STATUS = 0 and 
		<![CDATA[SYSDATE < c.START_TIME]]>
		then 0 
		WHEN
		c.AUDIT_STATUS = 1 then 1
		WHEN
		c.AUDIT_STATUS = -1 then -1
		ELSE 4 
		END AUDIT_STATUS
	FROM
		T_COST_REGION_PRICE b
		
		left join T_BASE_EMPLOYEE e1
		on b.CREATE_USER_CODE = e1.EMPLOYEE_CODE
		left join 
		(
			SELECT
				A .audit_time,
				A .audit_user_code,
				A .rele_id,
				A .AUDIT_status,
		    A.START_TIME,
		    A.audit_hour
			FROM
				(
					SELECT
						ROW_NUMBER () OVER (
							PARTITION BY T .rele_id
							ORDER BY
								T .audit_time ASC
						) AS rn,
						T .*
					FROM
						t_cost_price_ad T
						where 1=1
						
				) A
			WHERE
				A .rn = 1
		) c
		  on b.rele_id=c.rele_id
	left join T_BASE_EMPLOYEE e2
		  on c.audit_user_code =e2.EMPLOYEE_CODE
	WHERE 1=1
		<if test="auditStatus == 0" >
      		and c.AUDIT_STATUS = '0' and 
      		<![CDATA[SYSDATE < c.START_TIME]]>
        </if>
		<if test="auditStatus == 1" >
      		and c.AUDIT_STATUS = #{auditStatus,jdbcType=NUMERIC}
        </if>
		<if test="auditStatus == -1" >
      		and c.AUDIT_STATUS = #{auditStatus,jdbcType=NUMERIC}
        </if>
		<if test="auditStatus == 2" >
      		and c.AUDIT_STATUS = '0' and 
      		<![CDATA[SYSDATE > c.START_TIME]]>
        </if>
		 <!-- 报价名称 -->
      <if test="costName != null and costName !=''" >
      		AND b.COST_NAME like concat(concat('%',#{costName,jdbcType=VARCHAR}),'%')
      </if>
		 <!-- 产品类型 -->
      <if test="productCode != null and productCode !=''" >
      		AND b.PRODUCT_CODE = #{productCode,jdbcType=NUMERIC}
      </if>
		 <!-- 派送方式 -->
      <if test="disType != null and disType !='' or disType == 0" >
      		AND b.DIS_TYPE = #{disType,jdbcType=NUMERIC}
      </if>
		 <!-- 开始派货区间 -->
      <if test="starRegionId != null and starRegionId !='' and starRegionId !='empty'" >
      		AND b.STAR_REGION_ID = #{starRegionId,jdbcType=VARCHAR}
      </if>
		 <!-- 结束派货区间 -->
      <if test="endRegionId != null and endRegionId !='' and endRegionId !='empty'" >
      		AND b.END_REGION_ID = #{endRegionId,jdbcType=VARCHAR}
      </if>
		 <!-- 业务类型 -->
      <if test="bizType != null and bizType !=''" >
      		AND b.BIZ_TYPE = #{bizType,jdbcType=NUMERIC}
      </if>
		 <!-- 启用状态-->
      <if test="delFlag != null and delFlag !=''" >
      		AND b.DEL_FLAG = #{delFlag,jdbcType=NUMERIC}
      </if>
		 <!-- 产品时效-->
      <if test="timeCode != null and timeCode !=''" >
      		AND b.TIME_CODE = #{timeCode,jdbcType=NUMERIC}
      </if>
		 <!-- 计费类型-->
      <if test="calcType != null and calcType !=''" >
      		AND b.CALC_TYPE = #{calcType,jdbcType=NUMERIC}
      </if>
      <if test="startTimeStart != null and startTimeEnd != null">
          and <![CDATA[ b.START_TIME  >= #{startTimeStart,jdbcType=TIMESTAMP}]]>
          and <![CDATA[ b.START_TIME  <= #{startTimeEnd,jdbcType=TIMESTAMP}]]>
      </if>
      <if test="endTimeStart != null and endTimeEnd != null">
          and <![CDATA[ b.END_TIME  >= #{endTimeStart,jdbcType=TIMESTAMP}]]>
          and <![CDATA[ b.END_TIME  <= #{endTimeEnd,jdbcType=TIMESTAMP}]]>
      </if>
         <!-- 计费类型-->
      <if test="priceItemCode != null and priceItemCode !=''" >
      		AND b.PRICE_ITEM_CODE = #{priceItemCode,jdbcType=NUMERIC}
      </if>
      order by b.CREATE_TIME desc
  </select>

  

SELECT b.AUDIT_HOUR, b.OPER_STATUS, b.RELE_ID, b.STAR_REGION_ID, b.END_REGION_ID, b.START_REGION__NAME, b.END_REGION_NAME, b.CREATE_USER_CODE||'('||e1.EMPLOYEE_NAME||')' CREATE_USER_CODE, b.MODIFY_USER_CODE, b.CREATE_TIME, b.MODIFY_TIME, b.DEL_FLAG, b.REMARK, b.COST_NAME, b.START_REGION_TYPE, b.END_REGION_TYPE, b.CALC_TYPE, b.BIZ_TYPE, b.TIME_CODE, b.PRODUCT_CODE, b.DIS_TYPE, b.START_TIME, b.END_TIME, b.PRICE_ITEM_CODE, c.audit_time, c.audit_user_code||'('||e2.EMPLOYEE_NAME||')' audit_user_code FROM T_COST_REGION_PRICE b left join T_BASE_EMPLOYEE e1 on b.CREATE_USER_CODE = e1.EMPLOYEE_CODE left join ( SELECT A .audit_time, A .audit_user_code, A .rele_id, A .AUDIT_status, A.START_TIME, A.audit_hour FROM ( SELECT ROW_NUMBER () OVER ( PARTITION BY T .rele_id ORDER BY T .audit_time ASC ) AS rn, T .* FROM t_cost_price_ad T where 1=1 ) A WHERE A .rn = 1 ) c on b.rele_id=c.rele_id left join T_BASE_EMPLOYEE e2 on c.audit_user_code =e2.EMPLOYEE_CODE WHERE 1=1

  

SELECT
                A .audit_time,
                A .audit_user_code,
                A .rele_id,
                A .AUDIT_status,
            A.START_TIME,
            A.audit_hour
            FROM
                (
                    SELECT
                        ROW_NUMBER () OVER (
                            PARTITION BY T .rele_id
                            ORDER BY
                                T .audit_time ASC
                        ) AS rn,
                        T .*
                    FROM
                        t_cost_price_ad T
                        where 1=1
                        
                ) A
            WHERE
                A .rn = 1



一对多,(多)条件符合,取(多)时间最近的信息

原文地址:https://www.cnblogs.com/acme6/p/9259594.html