2015-07-19 67 views
-2

我在Visual Studio中使用此查詢。我想添加另一列,將我找到的字段(HistoryActsFormsDocuments)合計爲每個工作人員的Total。我試圖添加Sum,但效果不佳。如何向我的SQL查詢添加總和?

我該怎麼做?

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

決不連接字符串創建SQL查詢。這是一個安全隱患。改爲使用參數化查詢。 –

回答

0

要顯示總數還是計數? 我想你需要做類似如下:

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

您還需要通過功能使用一組:

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

+0

我想要顯示計數(正如我在查詢中所寫的那樣)並在查詢結尾處添加另一個左外部聯接,它們總計我之前統計的所有字段。 – user3885474