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

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的行(如果存在),然后保留max date(Attribute2),如果这不是一行,则保留max 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

我试图用嵌套连接来实现这一点,但这似乎过于复杂。
一些简单的解决方案是,首先按以下顺序执行SQL结果:
CREATE TABLE output AS
SELECT
    ID,
    Name,
    Attribute1,
    Attribute2,
    Attribute3
FROM input
ORDER BY
    Name,
    Attribute1 DESC,
    Attribute2 DESC,
    Attribute3 DESC;

对每一行执行循环,检查并缓存名称是否出现在之前-如果没有,则保留它(以及某些全局变量中的缓存名称),否则删除行。
还有其他纯SQL解决方案吗?

最佳答案

对于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

08-07 05:10