如何获取重复值基于条件

时间:2015-06-08 11:42:22

标签: sql-server sql-server-2008

我有一张如下表:

id     pid    status
10     1      0
11     1      0
10     2      0
15     1      0
11     2      0
10     3      0

我需要基于id值的常见pid。这意味着所有id的常见pids值。

所以在上表中预期的输出是:

id    
10 

我曾尝试过以下查询,但它无法按预期运行:

SELECT DISTINCT id
FROM mytable COUNT(1)
FROM mytable
WHERE pid IN (1,2,3)
GROUP BY checkpointid

1 个答案:

答案 0 :(得分:4)

试试这个:

SELECT Id
FROM Tbl
GROUP BY Id
HAVING COUNT(DISTINCT Pid) = (
  SELECT COUNT(DISTINCT Pid)
  FROM Tbl
)