-1

我的表是这样的......


**AttName**     **Title**        **Count_Index**

Red                Boys              1
Red                Girls             2
Green              Boys              1
Blue               Boys              1

我只想回来...

Red  Boys        1
Red  Girls       2

那是因为我有两个条目的红色,如果它们的计数仅为 1,我想跳过/删除所有行。换句话说,如果它们的计数高于“1”,我只对行感兴趣。

4

2 回答 2

2

尝试

SELECT * 
  FROM table1
 WHERE AttName IN (SELECT AttName FROM table1 GROUP BY AttName HAVING COUNT(*) > 1)

SQLFiddle

输出

| ATTNAME | TITLE | COUNT_INDEX |
---------------------------------
|     Red |  Boys |           1 |
|     Red | Girls |           2 |
于 2013-05-02T22:41:59.147 回答
0

好的,这是经过测试的。在查找重复项时,我喜欢使用窗口函数。特别是因为避免在 where 子句中进行子选择,并从同一个表中进行两次。相反,所有需要的列都已在子选择中拉出。尽管有时窗口功能可能很昂贵。

Select *, ROW_NUMBER() over (Partition by AttrName Order By AttrName) --probably better to order by whatever the primary key is for consistent results, esepcially if you plan to use this in a delete statement
From (
  SELECT AttName, title, COUNT(AttrName) over (partition by AttrName) as cnt
  FROM yourtable
) as counted
Where counted.cnt > 1
于 2013-05-02T22:38:41.840 回答