Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中使用dinaymic查询按列计算总和_Sql_Sql Server 2008 - Fatal编程技术网

在SQL Server中使用dinaymic查询按列计算总和

在SQL Server中使用dinaymic查询按列计算总和,sql,sql-server-2008,Sql,Sql Server 2008,我需要计算列查询的SUM 是否可以使用while循环获取列计数?您可能希望对Narea列进行SUM(),按您的id列进行分组 id varname 1area 2area 3area 4area ------------------------------------ 1 abc 345 3.7 34 87 1 pqr 46 67 78 55 1 lmn 67 99 33 44 2 x

我需要计算列查询的
SUM


是否可以使用while循环获取列计数?

您可能希望对
Narea
进行
SUM()
,按
您的
id
列进行分组

 id varname 1area 2area 3area 4area
 ------------------------------------
 1   abc      345  3.7   34     87
 1   pqr      46    67   78     55 
 1   lmn      67   99    33     44 
 2   xyz      78    78    33    32

您的结果集是什么样的?
select id, sum(1area),
sum(2area), sum(3area), sum(4area)
from tbl1
group by id;