sql-server – 对select查询返回的行进行计数

前端之家收集整理的这篇文章主要介绍了sql-server – 对select查询返回的行进行计数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要计算以下查询返回的行数.
select m.Company_id
from Monitor as m
    inner join Monitor_Request as mr on mr.Company_ID=m.Company_id
    group by m.Company_id
    having COUNT(m.Monitor_id)>=5

我已经尝试过跟随

select COUNT(*) from
(
select m.Company_id
from Monitor as m
    inner join Monitor_Request as mr on mr.Company_ID=m.Company_id
    group by m.Company_id
    having COUNT(m.Monitor_id)>=5)

它在查询分析器中提供了一个错误消息,其中说明如下:

Msg 102,Level 15,State 1,Line 7 Incorrect Syntax near ‘)’.

解决方法

sql Server需要您SELECT FROM或JOIN的子查询才能使用别名.

在您的子查询添加一个别名(在本例中为x):

select COUNT(*) from
(
select m.Company_id
from Monitor as m
    inner join Monitor_Request as mr on mr.Company_ID=m.Company_id
    group by m.Company_id
    having COUNT(m.Monitor_id)>=5)  x
原文链接:https://www.f2er.com/mssql/82559.html

猜你在找的MsSQL相关文章