【发布时间】:2014-02-19 05:24:30
【问题描述】:
下面是我针对 Postgres 数据库运行的查询。
select distinct(col1),col2,col3,col4
from tableNot
where col5 = 100 and col6 = '78'
order by col4 DESC limit 100;
下面是我在控制台上得到的输出-
col1 col2 col3 col4
entry.one 1.2.3 18 subject
entry.one 1.2.8 18 newSubject
entry.two 3.4.9 20 lifePerfect
entry.two 3.4.5 17 helloPartner
- 现在,如果您看到我上面的输出,
entry.one和entry.two将出现两次,所以我将比较col2的值与entry.one的值,以较高者为准,我将保留该行。因此,对于entry.one,我会将1.2.3与1.2.8进行比较,而1.2.8对于entry.one更高,所以我只会为entry.one保留这一行。 - 与
entry.two类似,我会将3.4.9与3.4.5进行比较,3.4.9对于entry.two更高,所以我将只为entry.two保留这一行。
下面是我想在控制台上看到的输出 -
col1 col2 col3 col4
entry.one 1.2.8 18 newSubject
entry.two 3.4.9 20 lifePerfect
这可以在 SQL 中实现吗?
P.S 任何 Fiddle 示例都会很棒。
【问题讨论】:
标签: sql postgresql comparison aggregate-functions