繁体   English   中英

需要帮助选择SQL中的distinct和max字段

[英]Need help selecting distinct and max field in sql

因此,在nc_sno.netcredit_dist_lp1表中,有贷款编号和ID编号。 每个贷款号最多可以有两个不同的ID号。 我想为每个贷款号提取最高的ID号。 这就是我所拥有的。 感谢您的帮助:

select
  tempkk3.loan_number
,tempkk3.username
, tempkk3.week_number
,lp.lp_rules_instruction as lp_instruction
,lp.insert_timestamp as time_entered_queue
,lp.id
from
  nc_sno.netcredit_dist_lp1 lp
join tempkk3 using (loan_number)

Postgres distinct on功能distinct on有非常不错的distinct on ,它确实可以实现您想要的功能:

select distinct on (loan_number) loan_number,
       tempkk3.username, tempkk3.week_number,
       lp.lp_rules_instruction as lp_instruction,
       lp.insert_timestamp as time_entered_queue,
       lp.id
from nc_sno.netcredit_dist_lp1 lp join
     tempkk3
     using (loan_number)
order by loan_number, id desc;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM