从内部选择语句计数

时间:2013-03-11 17:51:29

标签: sql sql-server select count

我试图在SQL服务器中执行以下查询但是我收到错误。有人可以解释一下如何计算内部选择语句吗?

SELECT COUNT(*) from

(Select ID,
      COUNT(ID)
  FROM [Testing].[dbo].[Bench] group by ID  having COUNT(*) =5);

2 个答案:

答案 0 :(得分:8)

我想你错过了第二栏的名字:

SELECT COUNT(*)
FROM (
    Select id, COUNT(id) count_of_id
    FROM [Testing].[dbo].[Bench]
    GROUP BY id
    HAVING COUNT(*) =5
);

答案 1 :(得分:5)

尝试以下代码:

SELECT COUNT(*) from

(Select ID,
      COUNT(ID) as cnt
  FROM [Testing].[dbo].[Bench] group by ID  having COUNT(*) =5) tbl;