简体   繁体   中英

Get Row Count with Group By statement SQL Server 2008

I am using SQL Server 2008 and navicat. I need to get Row Count in a table using SQL. The problem is that I am using Group By statement and my result is :

在此处输入图片说明

I need Sum of rcount value, the same like RowCount without Group By statement.

Thanks, in advance.

Assuming you want this as an additional column, you can do this using window functions:

select count(*) as rcount, goalarea,
       sum(count(*)) over () as TotalCount
from table t
group by goalarea;

If you want it as a separate row, I would use with rollup :

select count(*) as rcount, goalarea
from table t
group by goalarea with rollup;

(You can also use grouping sets , but I find with rollup easier for this simple problem.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM