我正在尝试返回给定数量的服务器名称,然后计算返回的数量。例如:
select servername, count(distinct servername) number
from TABLE
where [target] = 'blah'
group by servername
返回以下内容:
servername number
server1 1
server2 1
我试图得到的是“数字”列读取“2”,因为有2个不同的服务器名称。这应该非常简单,但我完全不知所措。
答案 0 :(得分:3)
您可以使用相关子查询:
select distinct servername,
(select count(distinct servername)
from yourtable
where target = 'blah') number
from yourtable
where target = 'blah'
答案 1 :(得分:0)
select servername,
count(*) over (partition by servername) as number
from the_table
where [target] = 'blah'
所有现代DBMS都支持窗口函数(over()
部分),包括SQL Server(您似乎正在使用它)