每组获得一个最大值

时间:2017-04-19 13:43:09

标签: sql postgresql sqlite

我需要在表格中为每组名称保留一行:

ID | Name  | Attribute1| Attribute2 | Attribute3
 1 | john  | true      | 2012-20-10 | 12345670
 2 | john  | false     | 2015-20-10 | 12345671
 3 | james | false     | 2010-02-01 | 12345672
 4 | james | false     | 2010-02-03 | 12345673
 5 | james | false     | 2010-02-06 | 12345674
 6 | sara  | true      | 2011-02-02 | 12345675
 7 | sara  | true      | 2011-02-02 | 12345676

......根据指定标准。首先应该保留在Attribute1中保存为true的行(如果存在),然后使用最大日期(Attribute2),如果那不是一行 - 具有最大Attribute3的行。

期望的结果是:

ID|Name|Attribute1|Attribute2|Attribute3
1 | john  | true  | 2012-20-10 | 12345670
5 | james | false | 2010-02-06 | 12345674
7 | sara  | true  | 2011-02-02 | 12345676

我试图用嵌套连接做到这一点,但这似乎过于复杂。 一些简单的解决方案是首先执行ORDER BY的SQL结果:

CREATE TABLE output AS
SELECT 
    ID, 
    Name,
    Attribute1,
    Attribute2,
    Attribute3
FROM input 
ORDER BY 
    Name,
    Attribute1 DESC, 
    Attribute2 DESC, 
    Attribute3 DESC;

并为每一行执行循环并检查并缓存以前是否出现名称 - 如果不是,则保留它(并在某些全局变量中缓存名称),否则删除行。

还有其他纯SQL解决方案吗?

1 个答案:

答案 0 :(得分:2)

对于Postgresql:

select distinct on (name) *
from t
order by name, attribute1 desc, attribute2 desc, attribute3 desc

https://www.postgresql.org/docs/current/static/sql-select.html#SQL-DISTINCT