仅当存在3个或更多个时,才拉出所有记录

时间:2015-04-27 13:21:34

标签: sql duplicates

如果两个字段(类别和度量)创建3个或更多重复项,我试图提取所有字段和记录。

SELECT category
      ,measure 
      ,date
FROM my_table

例如:

category measure date
EVENTS   COL     04/15/2014
EVENTS   COL     05/21/2014
EVENTS   COL     07/16/2014

所以上面的标准符合3或者更高标准,所以我们会把这三个标准拉下来。

category measure rec_count
EVENTS   COL     3

4 个答案:

答案 0 :(得分:1)

听起来像是在寻找GROUP BY

SELECT category
  ,measure 
  ,count(*) as rec_count
FROM my_table
GROUP BY category, measure
HAVING count(*) >= 3

答案 1 :(得分:1)

SELECT
    category, measure, date, count(*) cnt
FROM my_table
GROUP BY category, measure
HAVING cnt >= 3
;

答案 2 :(得分:1)

我认为你想要的东西 - 为你提供包含3条或更多记录的所有行

tableView

只获得你要运行的计数

SELECT 
  category,
  measure,
  date 
FROM 
  my_table t1 

  inner join (
    SELECT 
      category,
      measure
    FROM 
      my_table
    group by
      category,
      measure
    having
      count(*) >= 3
  ) t2 on
    t2.category = t1.category and
    t2.measure = t1.measure

答案 3 :(得分:0)

这不会对结果进行分组。

SELECT q.* FROM
    (SELECT
    category
    ,measure 
    ,date
    ,COUNT(*) OVER(PARTITION BY category,measure) [rec_count]
    FROM
    my_table) q
WHERE q.rec_count >= 3