SQL 中多列的总和
Sum of multiple columns in SQL
我有这样一份报告
ID Score 1 Score 2 Sum Average
1 5 5 10 5
2 7 6 13 6.5
3 4 8 12 6
. . . . .
. . . . .
. . . . .
Total 16 19 35 17.5
我有 Score 1
和 Score 2
,但我需要通过 Stimulsoft 获得 Sum
、Average
和 Total
。
如何使用 sum 计算每行中两列的总和?
由于似乎没有人回答,我会试一试。我不确定您需要的是 sql 查询或 Stimulsoft 咨询。我会试试前者:
Select cast(id as varchar(max)), score1, score2, score1+score2 as sum, (CONVERT(DECIMAL(10,2),score1)+score2)/2 as average
from yourtable
UNION ALL
Select 'Total', (select sum(score1) from yourtablename) as Score1Total,(select sum(score2) from yourtablename) as Score2Total, (select sum(score1)+sum(score2) from yourtablename) as TotalSum, (select (CONVERT(DECIMAL(10,2),sum(score1))+sum(score2))/count(*)) from yourtablename as TotalAverage
应该 return 你发布的内容。但是,至少可以说,return将最后一行(总计)作为查询的一部分是糟糕的设计。这应该在下一步中完成,当您实际 display/generate 一个报告文件时。
希望此 Sql 查询对您有所帮助:
Select CAST(ID AS VARCHAR(10)) AS [ID], Score1, Score2, (Score1 + Score2) AS [SUM], ((Score1 + Score2)/2) AS [AVG] from Yourtable
UNION ALL
Select 'Total', SUM(Score1), SUM(Score2), SUM((Score1 + Score2)), SUM(((Score1 + Score2)/2)) from Yourtable
如果是经常使用的查询,您还可以查看 computed column 求和和平均列。
您应该在文本组件中使用下一个表达式:
{Sum(Score1+Score2)}
我有这样一份报告
ID Score 1 Score 2 Sum Average
1 5 5 10 5
2 7 6 13 6.5
3 4 8 12 6
. . . . .
. . . . .
. . . . .
Total 16 19 35 17.5
我有 Score 1
和 Score 2
,但我需要通过 Stimulsoft 获得 Sum
、Average
和 Total
。
如何使用 sum 计算每行中两列的总和?
由于似乎没有人回答,我会试一试。我不确定您需要的是 sql 查询或 Stimulsoft 咨询。我会试试前者:
Select cast(id as varchar(max)), score1, score2, score1+score2 as sum, (CONVERT(DECIMAL(10,2),score1)+score2)/2 as average
from yourtable
UNION ALL
Select 'Total', (select sum(score1) from yourtablename) as Score1Total,(select sum(score2) from yourtablename) as Score2Total, (select sum(score1)+sum(score2) from yourtablename) as TotalSum, (select (CONVERT(DECIMAL(10,2),sum(score1))+sum(score2))/count(*)) from yourtablename as TotalAverage
应该 return 你发布的内容。但是,至少可以说,return将最后一行(总计)作为查询的一部分是糟糕的设计。这应该在下一步中完成,当您实际 display/generate 一个报告文件时。
希望此 Sql 查询对您有所帮助:
Select CAST(ID AS VARCHAR(10)) AS [ID], Score1, Score2, (Score1 + Score2) AS [SUM], ((Score1 + Score2)/2) AS [AVG] from Yourtable
UNION ALL
Select 'Total', SUM(Score1), SUM(Score2), SUM((Score1 + Score2)), SUM(((Score1 + Score2)/2)) from Yourtable
如果是经常使用的查询,您还可以查看 computed column 求和和平均列。
您应该在文本组件中使用下一个表达式:
{Sum(Score1+Score2)}