选择Count(*)和其他列

时间:2014-03-17 11:51:40

标签: ms-access

Select count(*) from (Select ID from tblCustomer where ABC=XYZ...)

=>返回计数(*)= 2。

Select count(*), ID from (Select ID from tblCustomer where ABC=XYZ...)

=>返回错误:您尝试执行的查询不包含指定的表达式' ID'作为聚合函数的一部分

Select count(*), ID from (Select ID from tblCustomer where ABC=XYZ...) group by ID

=>返回计数(*)= 1。

我需要的是返回两者:ID列和所有记录查询的计数,如第一个查询。请帮帮我。

编辑:

Select (select count(*) from tblCustomer where ABC=XYZ) as total_count, 
       ID 
from tblCustomer where ABC=XYZ

如果我按照juergen的说法运行此命令,似乎计算机将运行2个查询:从tblCustomer中选择count(*),其中ABC = XYZ .... 并选择ID来自tblCustomer,其中ABC = XYZ 。它复制了查找条件'其中ABC = XYZ' 并将使计算机找到它2次。这不是我的表现选择,其他方式对性能有好处吗?

3 个答案:

答案 0 :(得分:2)

Select (select count(*) from tblCustomer) as total_count, 
       ID 
from tblCustomer 

答案 1 :(得分:2)

这是你的意思吗?

select count(*) over () as total count, id from tblCustomer
where ABC=XYZ

答案 2 :(得分:0)

试试这个: -

Select ID,(select count(*) from tblCustomer) as recordcount from tblCustomer;

在上面的查询中,我们使用了子查询。但是,根据我的知识,不推荐使用子查询来提高性能。