Sql:从(选择...)中选择count(*)

时间:2014-04-09 18:58:39

标签: sql sql-server count subquery

我有一个带分组的sql select命令,我想得到总行数。我如何实现这一目标?

我的sql命令:

select p.UserName, p.FirstName + ' ' + p.LastName as [FullName]
 ,count(b.billid) as [Count], sum(b.PercentRials) as [Sum] from Bills b
inner join UserProfiles p on b.PayerUserName=p.UserName
 where b.Successful=1
 group by p.UserName, p.FirstName + ' ' + p.LastName

我试过这些没有运气:

select count(*) from (select ...)

select count(select ...)

修改

这是我想要运行的完整的sql语句:

select count(*) from (    select p.UserName, p.FirstName + ' ' + p.LastName as [FullName]
 ,count(b.billid) as [Count], sum(b.PercentRials) as [Sum] from Bills b
inner join UserProfiles p on b.PayerUserName=p.UserName
 where b.Successful=1
 group by p.UserName, p.FirstName + ' ' + p.LastName)

我在最后一行收到此错误:

Incorrect syntax near ')'.

5 个答案:

答案 0 :(得分:2)

SELECT COUNT(*)
FROM 
(
select p.UserName, p.FirstName + ' ' + p.LastName as [FullName]
 ,count(b.billid) as [Count], sum(b.PercentRials) as [Sum] from Bills b
inner join UserProfiles p on b.PayerUserName=p.UserName
 where b.Successful=1
 group by p.UserName, p.FirstName + ' ' + p.LastName   --<-- Removed the extra comma here
) A  --<-- Use an Alias here

正如我所预期的那样,你错过了别名

select count(*) 
from (select ...) Q  --<-- This sub-query in From clause needs an Alias 

修改

如果您只需要知道此查询返回的行,并且您正在代码中执行此查询,则只需使用@@ROWCOUNT函数即可。像......那样......

SELECT ......     --<-- Your Query

SELECT @@ROWCOUNT    --<-- This will return the number of rows returned 
                       -- by the previous query 

答案 1 :(得分:1)

试试这段代码:

SELECT COUNT(*)
    FROM (
           SELECT p.UserName
               ,p.FirstName + ' ' + p.LastName AS [FullName]
               ,COUNT(b.billid) AS [Count]
               ,SUM(b.PercentRials) AS [Sum]
            FROM Bills b
            INNER JOIN UserProfiles p
                ON b.PayerUserName = p.UserName
            WHERE b.Successful = 1
            GROUP BY p.UserName
               ,p.FirstName + ' ' + p.LastName
         ) a

根据您的修改。您缺少派生的表别名。

如果查看FROM子句语法,您将看到

  

| derived_table [AS] table_alias [(column_alias [,... n])]

     

当派生表,行集或表值函数或运算符时   使用了子句(例如PIVOT或UNPIVOT),必需的table_alias at   子句的结尾是所有列的关联表名,   包括分组列,返回。

http://technet.microsoft.com/en-us/library/ms177634.aspx

答案 2 :(得分:1)

您的子查询

后缺少别名
select count(*) from (select ...) v

您还可以尝试使用现有SQL返回总计数,而不使用子查询

select p.UserName, 
p.FirstName + ' ' + p.LastName as [FullName],
count(b.billid) as [Count], 
sum(b.PercentRials) as [Sum],
COUNT(*) over () [TotalCount] ------- total count here
from Bills b
inner join UserProfiles p on b.PayerUserName=p.UserName
where b.Successful=1
group by p.UserName, p.FirstName + ' ' + p.LastName

答案 3 :(得分:1)

如果你有一个唯一的列名,你可以计算。例如,我假设UserName在这里是唯一的。

select count(query.UserName) from ( 
select p.UserName, p.FirstName + ' ' + p.LastName as [FullName]
 ,count(b.billid) as [Count], sum(b.PercentRials) as [Sum] from Bills b
inner join UserProfiles p on b.PayerUserName=p.UserName
 where b.Successful=1
 group by p.UserName, p.FirstName + ' ' + p.LastName) as query

答案 4 :(得分:-2)

COUNT() - 聚合函数

SELECT SQL_CALC_FOUND_ROWS * FROM ...; SELECT FOUND_ROWS(); //下一个查询!!!!