0

我有这个 sql 查询:

select COUNT(batch_id) AS cc FROM tbl_associate_593932 WHERE induction_result>50 GROUP BY batch_id

此查询仅使用一列 cc 即可正确执行。现在我想从该列中的值中获取最大值。我试过这样的事情:

select max (cc) from  (select COUNT(batch_id) AS cc FROM tbl_associate_593932 WHERE induction_result>50 GROUP BY batch_id);

但是在 SQL Server 中,它给了我错误:';' 附近的语法不正确。期望 AS、ID 或 Quoted_ID

和另一个错误无效的列名'cc'。

如何在 SQL Server 中执行此操作。在Oracle中也可以这样做吗?

4

2 回答 2

0

你可以这样做:

WITH CTE
AS
(
   select COUNT(batch_id) AS cc 
   FROM tbl_associate_593932 
   WHERE induction_result > 50 
   GROUP BY batch_id
) 
SELECT MAX(cc)
FROM CTE;

或者:

SELECT TOP(1)
  COUNT(barch_id) AS cc
FROM tbl_associate_593932 
WHERE induction_result > 50 
GROUP BY batch_id
ORDER BY cc DESC;
于 2013-01-09T17:50:25.940 回答
0

使用:select max (t.cc) from (select COUNT(batch_id) AS cc FROM tbl_associate_593932 WHEREduction_result>50 GROUP BY batch_id) as t;

于 2013-01-16T14:56:43.017 回答