Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 按行获取多个指标之和_Sql_Sql Server 2005 - Fatal编程技术网

Sql 按行获取多个指标之和

Sql 按行获取多个指标之和,sql,sql-server-2005,Sql,Sql Server 2005,社区新手,在该主题方面经验有限。我试图创建一个列,逐行获取指标的总和。因此,该列将对每个指标进行合计,第一个客户的合计值为3,第二个客户的合计值为2。使用Microsoft Sql Server管理工作室。任何帮助都将不胜感激 Customer Date Ind1 Ind2 Ind3 Ind4 12345 1-1-15 1 0 1 1 12346 1-2-15 0 1

社区新手,在该主题方面经验有限。我试图创建一个列,逐行获取指标的总和。因此,该列将对每个指标进行合计,第一个客户的合计值为3,第二个客户的合计值为2。使用Microsoft Sql Server管理工作室。任何帮助都将不胜感激

Customer      Date       Ind1   Ind2   Ind3    Ind4
 12345       1-1-15       1     0      1       1
 12346       1-2-15       0     1      1       0

选择客户、日期、Ind1+Ind2+Ind3+Ind4作为您可以使用的指标

SELECT Customer
       , Date
       , Ind1
       , Ind2
       , Ind3
       , Ind4
       , Ind1+Ind2+Ind3+Ind4 As Indicators
  FROM TABLE_NAME
表\u NAME
替换为表的任何名称。如果不希望报告所有
Ind1、Ind2、Ind3、Ind4列,请使用

SELECT Customer
       , Date
       , Ind1+Ind2+Ind3+Ind4 As Indicators
  FROM TABLE_NAME
你是说:

select customer,date, ind1+ind2+ind3 as Indicators from table_name order by Indicators
注意:您的列可能有
null
值,因此请使用以下选项:

select customer,date, isnull(ind1,0)+isnull(ind2,0)+isnull(ind3,0) as Indicators
from table_name order by Indicators

这假设Ind列的默认值为0,如果这些列可为null,则需要替换为isnull(Ind,0)谢谢!这个解决方案和其他解决方案结合在一起对我起了作用。