1

基本上我做了查询,但它没有工作'Msg 156,Level 15,State 1,Line 2 关键字'FROM'附近的语法不正确'

SELECT COUNT(*)
FROM
(
SELECT Group_ID, count(ID)
FROM dbo.S1_Users
WHERE Service_Plan = 'Multi User Foreign (B)'
GROUP BY Group_ID
HAVING count(ID) = 1
)

我知道它很容易修复,我搜索了谷歌但没有找到合适的

任何机构都可以对此进行解释吗?

4

2 回答 2

5

您需要命名虚拟表并count(ID)从内部查询中命名列

SELECT COUNT(*)
FROM
(
SELECT Group_ID, count(ID) as ID_Count
FROM dbo.S1_Users
WHERE Service_Plan = 'Multi User Foreign (B)'
GROUP BY Group_ID
HAVING count(ID) = 1
) as A

您也可以完全删除计数感觉您没有使用它

SELECT COUNT(*)
FROM
(
SELECT Group_ID
FROM dbo.S1_Users
WHERE Service_Plan = 'Multi User Foreign (B)'
GROUP BY Group_ID
HAVING count(ID) = 1
) as A
于 2013-06-11T20:05:20.160 回答
5

试试这样:

SELECT COUNT(*)
FROM
(
    SELECT Group_ID, count(ID) As cnt
    FROM dbo.S1_Users
    WHERE Service_Plan = 'Multi User Foreign (B)'
    GROUP BY Group_ID
    HAVING count(ID) = 1
) A
于 2013-06-11T20:05:55.867 回答