Postgres arg min in a group by

时间:2018-03-02 21:05:35

标签: sql postgresql

我正在使用Postgres 9.6,我有以下数据:

user, top_color
Alice, blue
Bob, red

我希望我想要的输出是:

WITH top_rank AS (
       SELECT "user", min(rank) AS rank FROM table GROUP BY user
),
SELECT "user", color 
FROM table 
JOIN top_rank on table.rank = top_rank.rank GROUP BY "user";

我尝试了什么:

const SwearWords = ["a##","ba##ard","bi###","c#ck","c#nt","d#ck","f#ck","gay","k#ke","n#gg","omfg","sh#t","wtf"];

是否有更简单的方法(理想情况下是1个查询,使用类似argmin的运算符)或者我是否需要使用2个查询?

1 个答案:

答案 0 :(得分:1)

我认为最简单的方法是public static LinkedList sieve(int n) { BitSet b = new BitSet(n); LinkedList ps = new LinkedList(); b.set(0,n); for (int p=2; p<n; p++) { if (b.get(p)) { ps.add(p); for (int i=p+p; i<n; i+=p) { b.clear(i); } } } return ps; }

distinct on