Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何在SQL server中对特定别名进行计数(*)?_Sql Server - Fatal编程技术网

Sql server 如何在SQL server中对特定别名进行计数(*)?

Sql server 如何在SQL server中对特定别名进行计数(*)?,sql-server,Sql Server,我有一个SQL命令,其中添加了多个表,并将LEFT JOIN table1作为别名。我想计算表1中的所有行,但不想计算表2 在只有一个表的命令中,我只需执行count(*)。然而,这里,*指的是所有连体表中的所有行table1.*仅获取该表中的所有行,但count(table1.*)在“*”附近抛出错误语法。错误 这是语法问题还是更深层次的问题?如何获得所需的功能 运行Microsoft SQL Azure(RTM)-12.0.2000.8而不是这样的查询 select *,count(C.Cu

我有一个SQL命令,其中添加了多个表,并将
LEFT JOIN table1作为别名
。我想计算
表1
中的所有行,但不想计算
表2

在只有一个表的命令中,我只需执行
count(*)
。然而,这里,
*
指的是所有连体表中的所有行
table1.*
仅获取该表中的所有行,但
count(table1.*)
在“*”附近抛出
错误语法。
错误

这是语法问题还是更深层次的问题?如何获得所需的功能


运行Microsoft SQL Azure(RTM)-12.0.2000.8而不是这样的查询

select *,count(C.CustomerID) over (partition by U.UserID)
from Users U
left join Customers C on C.ManagerID = U.UserID
where U.UserID between  1574 and 1580
select *
from Users U
outer apply (
        select Cnt = count(*) from  Customers 
        where ManagerID = U.UserID ) res
where U.UserID between  1574 and 1580
您可以像这样使用查询

select *,count(C.CustomerID) over (partition by U.UserID)
from Users U
left join Customers C on C.ManagerID = U.UserID
where U.UserID between  1574 and 1580
select *
from Users U
outer apply (
        select Cnt = count(*) from  Customers 
        where ManagerID = U.UserID ) res
where U.UserID between  1574 and 1580

应该是简单的
COUNT(*)
为什么不使用列名称(如
COUNT(table1.columFromTable1)
)来计算table1的行数呢?如果您也可以显示查询,这将很有帮助。您可以使用外部应用而不是左连接,并在应用表达式中计算计数。取决于你应该数什么。。。。