如何在postgres中COUNT个重复的行?

时间:2011-11-14 23:01:24

标签: sql postgresql histogram

我希望能够从包含两个整数值的元组中创建直方图。 使用postgresql。

这是查询:

 SELECT temp.ad_id, temp.distance  as hits FROM ( 
 'UNION ALL .join(cupound_query)' # python
) as temp GROUP BY temp.ad_id,temp.distance 

编辑:我想要的更好的例子

对于此输入:

(51, 5)
(51, 0)
(51, 3)
(51, 0)
(88, 2)
(88, 2)
(88, 2)
(84, 1)
(81, 9)

将是:

 (88,2) : 3
 (51,0) : 2
 (51,3) : 1
 (51,5) : 1
 (84,1) : 1
 (81,9) : 1

如何创建这些值的直方图? 换句话说,我怎么能计算多少次 一行有重复吗?

由于

3 个答案:

答案 0 :(得分:13)

您的问题留有解释空间。此测试用例显示了2个嵌套步骤:

CREATE TEMP TABLE x (ad_id int, distance int);
INSERT INTO x VALUES
 (510, 0),(956, 3),(823, 3),(880, 2)
,(523, 3),(467, 0),(843, 1),(816, 9)
,(533, 4),(721, 7),(288, 3),(900, 3)
,(526, 9),(750, 7),(302, 8),(463, 6)
,(742, 8),(804, 2),(62,  7),(880, 2)
,(523, 3),(467, 0),(843, 1),(816, 9)
,(533, 4),(721, 7),(288, 3),(900, 3)
,(526, 9),(750, 7),(302, 8),(816, 9)
,(533, 4),(721, 7),(288, 3),(900, 3)
,(533, 4),(721, 7),(288, 3),(396, 5);

每个值有多少重复项?

SELECT ad_id, count(*) AS ct FROM x GROUP BY 1;

结果:

ad_id  | ct
-------+----
62     | 1
288    | 4
302    | 2
396    | 1
...

阅读:ad_id 62存在1次,ad_id 288存在4次,...


“我如何计算一行重复的次数?”

SELECT ct
      ,COUNT (*) AS ct_ct
FROM   (SELECT ad_id, COUNT (*) AS ct FROM x GROUP  BY 1) a
GROUP BY 1
ORDER BY 1;

结果:

 ct | ct_ct
----+---
1   | 8
2   | 7
3   | 2
4   | 3

阅读:8次出现“ad_id是唯一的”,7次出现“2行同样ad_id”,...

答案 1 :(得分:2)

只需将count(*)添加到您的选择中:

SELECT temp.ad_id, temp.distance as hits, count(*)
....

答案 2 :(得分:-1)

下面是我写的关于如何使用Postgres直接在SQL中生成直方图的教程:

Simple Histograms in SQL

我认为您可以轻松地将此示例调整为您的表结构。