Question

I have a sql select command with grouping and I want to get the number of total rows. How do I achieve that?

My sql command:

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

I have tried these with no luck:

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

and

select count(select ...)

EDIT

this is the complete sql statement that I want to run:

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)

and I get this error on the last line:

Incorrect syntax near ')'.
Was it helpful?

Solution

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

As I expected from your shown attempt you were missing an Alias

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

Edit

If you only need to know the rows returned by this query and you are executing this query anyway somwhere in your code you could simply make use of @@ROWCOUNT function. Something like....

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

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

OTHER TIPS

You are missing an alias after your subquery

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

You can also try to return total count using existing SQL, without using subquery

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

Try this code:

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

based on your edit. You were missing derived tabled alias.

If you look at FROM clause syntax you will see

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

When a derived table, rowset or table-valued function, or operator clause (such as PIVOT or UNPIVOT) is used, the required table_alias at the end of the clause is the associated table name for all columns, including grouping columns, returned.

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

If you have a unique column name in there, you can count that. For example I'm assuming UserName is unique here.

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

COUNT() - aggregate function

SELECT SQL_CALC_FOUND_ROWS * FROM ...; SELECT FOUND_ROWS(); // next query !!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top