如何在我的SQL查询中添加总和?

时间:2015-07-19 08:44:55

标签: c# sql

我在Visual Studio中使用此查询。我想添加另一列,将我找到的字段(HistoryActsFormsDocuments)与每个工作人员的Total相加。我试图添加Sum但它并没有很好地运作。

我该怎么做?

sqlcmd.CommandText = "use odlight select e1.fullname as worker,acts,forms,documents,history from opg.dbo.vwemployee as e1 " +
                     " left outer join (select tsCreatedBy ,count(actid) as acts from hozactions as h1 where ((h1.tscreatedate between '" + date1 + "' and '" + date2 + "' ) or (h1.tsModifydate between '" + date1 + "' and '" + date2 + "')) group by tsCreatedBy ) as t1 on e1.UserID= t1.tsCreatedBy " +
                     " left outer join (select tsCreatedBy ,count(*) as forms from TikForms as t1,SysForms as s1 where t1.FormCounter=s1.Counter and t1.actioncounter in ('0','-1') and tsCreateDate between '" + date1 + "' and '" + date2 + "' group by tsCreatedBy ) as t2 on e1.UserID=t2.tsCreatedBy " +
                     " left outer join (select tsCreatedBy,count(*) as documents from Documents where createDate between '" + date1 + "' and '" + date2 + "' group by tsCreatedBy) as t3 on e1.UserID=t3.tsCreatedBy " +
                     " left outer join (select tsCreatedBy,count(*) as history from nispah where tscreatedate between '" + date1 + "' and '" + date2 + "' group by tsCreatedBy) as t5 on e1.UserID=t5.tsCreatedBy " +
                     " where e1.DepartmentCode not in (6,2,4,10) and e1.Active =1 order by e1.fullname ";

1 个答案:

答案 0 :(得分:0)

您想要显示总和还是计数? 我想你需要做以下的事情:

select worker, count(History), count (document), count(acts), count(forms) 
from ....
group by workers

您还需要按功能使用分组:

http://sql.sh/fonctions/agregation/sum