【发布时间】:2018-11-08 22:47:22
【问题描述】:
我有一个问题:
select distinct(donorig_cdn),cerhue_num_rfa,max(cerhue_dt) from t_certif_hue
group by donorig_cdn,cerhue_num_rfa
order by donorig_cdn
它返回了一些具有不同 cerhue_num_rfa 的重复 ID
我如何只为与日期最大值 (cerhue_dt) 匹配的 cerhue_num_rfa 的重复 id 返回一行......并且最后只有 10 个结果而不是 15 个?
【问题讨论】:
-
distinct是 NOT 一个函数。它是一个适用于选择列表中的 all 列的运算符。distinct (a), b, c与distinct a,(b),c或distinct a,b,(c)完全相同。话虽如此:您可能想要distinct on ()。 postgresql.org/docs/current/static/sql-select.html#SQL-DISTINCT -
看起来像一个经典的
top-n-per-group问题。 -
虽然对于这个小问题来说并不重要,但最好的做法是在问题中包含表定义和示例数据。因此,每个人都可以重现您的行为并测试他们的解决方案。
标签: postgresql max distinct