检索单列唯一值的代表记录

时间:2011-11-16 15:55:18

标签: postgresql

对于Postgresql 8.x,我有一个包含answers的{​​{1}}表,其中(id, user_id, question_id, choice)是一个字符串值。我需要一个查询,它将为所有唯一choice值返回一组记录(返回的所有列)。我正在寻找的是每个独特选择的单一代表性记录。我还希望有一个聚合choice列,该列是与每条记录附带的每个唯一选择匹配的记录数的votes。我想强制count()为小写,以便进行此比较 HeLLo Hello 应该被视为相等)。我不能choice因为我想要结果集中的所有列。按所有列分组会导致返回所有记录,包括所有重复记录。

1。最近我得到了

GROUP BY lower(choice)

这个问题是它不会返回所有列。

select lower(choice), count(choice) as votes from answers where question_id = 21 group by lower(choice) order by votes desc;

2。尝试使用所有列

                     lower                     | votes 
-----------------------------------------------+-------
 dancing in the moonlight                      |     8
 pumped up kicks                               |     7
 party rock anthem                             |     6
 sexy and i know it                            |     5
 moves like jagger                             |     4

因为我没有指定select *, count(choice) as votes from answers where question_id = 21 group by lower(choice) order by votes desc; SELECT的每一列,所以这会引发错误,告诉我这样做。

3。指定GROUP BY

中的所有列
GROUP BY

对于所有记录,这只会将select *, count(choice) as votes from answers where question_id = 21 group by lower(choice), id, user_id, question_id, choice order by votes desc; 列的表格转储为votes

如何从 1。获取1计数和唯一代表性记录,但是返回表中的所有列?

2 个答案:

答案 0 :(得分:0)

将分组结果与主表结合起来,然后每个(问题,答案)组合只显示一行。

与此类似:

WITH top5 AS (
  select question_id, lower(choice) as choice, count(*) as votes 
  from answers 
  where question_id = 21 
  group by question_id , lower(choice) 
  order by count(*) desc
  limit 5
)
SELECT DISTINCT ON(question_id,choice) *
FROM top5 
JOIN answers USING(question_id,lower(choice))
ORDER BY question_id, lower(choice), answers.id;

答案 1 :(得分:0)

这是我最终的结果:

SELECT answers.*, cc.votes as votes FROM answers join (
    select max(id) as id, count(id) as votes 
    from answers
    group by trim(lower(choice))
) cc 
on answers.id = cc.id ORDER BY votes desc, lower(response) asc
相关问题