postgres窗口函数分页与排序问题

postgres由于使用了窗口函数,分页查询必须手动指定排序,否则拿到的数据永远是第一页

select id,vendor_id,vendor_cont_id,vendor_cont_code,goods_id,buyer_id,buyer_name,pur_dept_id,pur_dept_name,tenant_id,tenant_code,system_code,company_id 
  from 
  (
  select
      row_number() OVER (ORDER BY (SELECT NULL)) AS id,
  a.vendor_id,
  a.vendor_cont_id,
  cont.vendor_cont_code,
  b.goods_id,
  a.buyer_id,
	dce.emp_code buyer_code,
  dce.emp_name buyer_name,
  a.pur_dept_id,
	dhcoi.dept_code as pur_dept_code,
  dhcoi.dept_name as pur_dept_name,
  1224 as tenant_id,
  'guoyao-lerentang' as tenant_code,
  '1224' as system_code,
    a.owner_id as company_id,
    row_number() over (partition by
      a.owner_id,
      b.goods_id,
      a.vendor_id,
          a.system_code,
          a.vendor_cont_id 
          order by b.modify_date,b.create_date desc
          ) rn
  from
    dwi_pur_ps_channel_hdr a
  inner join dwi_pur_ps_channel_dtl b 
  on
    a.pur_chl_hdr_id = b.pur_chl_hdr_id
    and a.system_code = b.system_code
  left join sjzt_rebate_admin.dim_cms_emp dce 
  on
    a.buyer_id = dce.emp_id
    and a.system_code = dce.system_code
  left join sjzt_rebate_admin.dim_qms_goods_basic dqgb on
    a.system_code = dqgb.system_code
    and b.goods_id = dqgb.scs_goods_files_id
  left join sjzt_rebate_admin.dwi_hrs_cms_org_info dhcoi on
    a.pur_dept_id = dhcoi.dept_id
    and a.system_code = dhcoi.system_code
  left join dwi_qms_cvm_vend_cont_info cont on
    cont.vendor_cont_id = a.vendor_cont_id
    and cont.system_code = a.system_code
  where
    a.system_code = '1224'
    and a.owner_id = '18'
      and dce.stop_flag = 00
    and dce.logical_is_deleted = 0

  	and a.vendor_id = 3681
  	and a.vendor_cont_id = 60236
  	and b.goods_id = 10166
  ) tmp where tmp.rn = 1 limit 2000 offset 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值