Sql server 如何在SQL server中对特定别名进行计数(*)?
我有一个SQL命令,其中添加了多个表,并将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
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的行数呢?如果您也可以显示查询,这将很有帮助。您可以使用外部应用而不是左连接,并在应用表达式中计算计数。取决于你应该数什么。。。。