postgresql數據庫中的 rownum


公司使用的是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之后添加上排序的條件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM