6

我正在尝试在 SQL Server 中执行以下查询,但出现错误。有人可以解释一下如何计算内部选择语句吗?

SELECT COUNT(*) from

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

2 回答 2

9

我想您只是错过了第二列的名称:

SELECT COUNT(*)
FROM (
    Select id, COUNT(id) count_of_id
    FROM [Testing].[dbo].[Bench]
    GROUP BY id
    HAVING COUNT(*) =5
);
于 2013-03-11T17:54:35.110 回答
7

试试下面的代码:

SELECT COUNT(*) from

(Select ID,
      COUNT(ID) as cnt
  FROM [Testing].[dbo].[Bench] group by ID  having COUNT(*) =5) tbl;
于 2013-03-11T17:54:53.623 回答