简体   繁体   English

使用Group By语句获取行计数SQL Server 2008

[英]Get Row Count with Group By statement SQL Server 2008

I am using SQL Server 2008 and navicat. 我正在使用SQL Server 2008和navicat。 I need to get Row Count in a table using SQL. 我需要使用SQL在表中获取行数。 The problem is that I am using Group By statement and my result is : 问题是我正在使用Group By语句,结果是:

在此处输入图片说明

I need Sum of rcount value, the same like RowCount without Group By statement. 我需要rcount值的总和,就像不使用Group By语句的RowCount一样。

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 : 如果您希望将其作为单独的一行,我将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.) (您也可以使用grouping sets ,但是对于这个简单的问题,我发现with rollup更容易。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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