码迷,mamicode.com
首页 > 数据库 > 详细

postgresql数据库中的 rownum

时间:2020-06-11 23:21:01      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:data   info   gre   结果   排序   left join   content   postgre   start   

公司使用的是postgresql数据库

今天在写项目的时候,遇到了这样一个问题,自己写出的sql语句,如果一次性全部查询出来一共有24条数据,这24条数据也是按照bussinessdate进行降序排列的,显示的最近日期就是自己今天操作的,2020-06-11

但是如果自己加上分页,控制每页显示10条的化,就虽然查询出来的结果也是按照降序排列的,但是6-11号的数据就没有显示,很是奇怪,最后询问同事,解决了问题:

原先的代码:

    <select id="queryScoreDetailList" parameterType="com.picc.hmbms.outinterface.scoreDetail.vo.ScoreDetailInfoQueryVo" resultMap="reMemberScoreInfo_i">
        select *
        from (select tmp_page.*
        from (
        select distinct tsg.member_code   memberCode,
        row_number() OVER() as rownum,
        tsg.SCORE_TYPE  ,
        <!--  tsd.CONSUMEORG         businessOrg, -->
        <!-- tsd.GRANTORG         grantOrg,  -->
        tsd.GRENT_PROJECT,
        to_char(tsd.insert_time,‘yyyy-MM-dd‘) insertTime,
        to_char(tsg.end_date,‘yyyy-MM-dd‘) endDate,
        tss.order_no orderNo,
        tsg.content,
        (case when tsd.consumeorg is null then tsd.grantorg else tsd.consumeorg end ) businessOrg,
        tsd.SYS_NO         sysNo ,
        (case when tsd.SCORE is  null  then 0  else round(tsd.SCORE,0) end)     score,
        (case when tss.consumer_date is null then to_char(tsg.GRANT_DATE,‘yyyy-MM-dd hh24:mi:ss‘)
        else  to_char(tss.consumer_date,‘yyyy-MM-dd hh24:mi:ss‘) end) businessDate,
        tsd.BUSINESS_TYPE businessType
        from t_member_score_detail tsd
        left join t_score_grent tsg  on tsg.Member_Code = tsd.member_code   and tsg.SCORE_GRENT_ID = tsd.SCORE_GRENT_ID
        left join t_score_settel tss on tss.Member_Code = tsd.member_code   and tss.SCORE_SETTEL_ID = tsd.SCORE_SETTEL_ID
        <if test="memberCode != null and ‘‘ != memberCode">
            where tsd.MEMBER_CODE = #{memberCode, jdbcType=VARCHAR}
        </if>
        <if test="startDate != null and ‘‘!= startDate">
            AND ( to_date(to_char(tsd.GRANT_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ >= ]]>to_date(#{startDate},‘yyyy-MM-dd‘)
            or to_date(to_char(tsd.CONSUME_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘)<![CDATA[ >= ]]> to_date(#{startDate},‘yyyy-MM-dd‘))

        </if>
        <if test="endDate != null and ‘‘ != endDate">
            AND ( to_date(to_char(tsd.GRANT_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ <= ]]> to_date(#{endDate},‘yyyy-MM-dd‘)
            or to_date(to_char(tsd.CONSUME_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ <= ]]> to_date(#{endDate},‘yyyy-MM-dd‘))
        </if>
        order by businessDate desc ) tmp_page
        where rownum <![CDATA[ <= ]]> #{maxNum} and  tmp_page.businessDate is not null) lm
        where rownum > #{minNum}
  </select>

修改之后的代码:

<select id="queryScoreDetailList" parameterType="com.picc.hmbms.outinterface.scoreDetail.vo.ScoreDetailInfoQueryVo" resultMap="reMemberScoreInfo_i">
        select *
        from (select tmp_page.*
        from (
        select distinct tsg.member_code   memberCode,
        row_number() OVER (order by consumer_date )   AS    rownum,
        tsg.SCORE_TYPE  ,
        <!--  tsd.CONSUMEORG         businessOrg, -->
        <!-- tsd.GRANTORG         grantOrg,  -->
        tsd.GRENT_PROJECT,
        to_char(tsd.insert_time,‘yyyy-MM-dd‘) insertTime,
        to_char(tsg.end_date,‘yyyy-MM-dd‘) endDate,
        tss.order_no orderNo,
        case when tsd.score_type=‘06‘ then ‘挑战赢积分活动扣100积分‘ else tsg.CONTENT end,
        (case when tsd.consumeorg is null then tsd.grantorg else tsd.consumeorg end ) businessOrg,
        tsd.SYS_NO         sysNo ,
        (case when tsd.SCORE is  null  then 0  else round(tsd.SCORE,0) end)     score,
        (case when tss.consumer_date is null then to_char(tsg.GRANT_DATE,‘yyyy-MM-dd hh24:mi:ss‘)
        else  to_char(tss.consumer_date,‘yyyy-MM-dd hh24:mi:ss‘) end) businessDate,
        tsd.BUSINESS_TYPE businessType
        from t_member_score_detail tsd
        left join t_score_grent tsg  on tsg.Member_Code = tsd.member_code   and tsg.SCORE_GRENT_ID = tsd.SCORE_GRENT_ID
        left join t_score_settel tss on tss.Member_Code = tsd.member_code   and tss.SCORE_SETTEL_ID = tsd.SCORE_SETTEL_ID
        <if test="memberCode != null and ‘‘ != memberCode">
            where tsd.MEMBER_CODE = #{memberCode, jdbcType=VARCHAR}
        </if>
        <if test="startDate != null and ‘‘!= startDate">
            AND ( to_date(to_char(tsd.GRANT_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ >= ]]>to_date(#{startDate},‘yyyy-MM-dd‘)
            or to_date(to_char(tsd.CONSUME_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘)<![CDATA[ >= ]]> to_date(#{startDate},‘yyyy-MM-dd‘))

        </if>
        <if test="endDate != null and ‘‘ != endDate">
            AND ( to_date(to_char(tsd.GRANT_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ <= ]]> to_date(#{endDate},‘yyyy-MM-dd‘)
            or to_date(to_char(tsd.CONSUME_DATE, ‘yyyy-MM-dd‘), ‘yyyy-MM-dd‘) <![CDATA[ <= ]]> to_date(#{endDate},‘yyyy-MM-dd‘))
        </if>
        order by businessDate desc ) tmp_page
        where rownum <![CDATA[ <= ]]> #{maxNum} and  tmp_page.businessDate is not null) lm
        where rownum > #{minNum}
  </select>

总的来说:一定要在over之后添加上排序的条件。

postgresql数据库中的 rownum

标签:data   info   gre   结果   排序   left join   content   postgre   start   

原文地址:https://www.cnblogs.com/dongyaotou/p/13096689.html

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