SQL 實現(xiàn)某時間段的統(tǒng)計業(yè)務(wù)
更新時間:2013年01月17日 11:15:05 作者:
有一張錯誤上報表,現(xiàn)在要做的是統(tǒng)計在某個時間段[beginTime,endTime](其中beginTime,endTime由前臺進行傳入)內(nèi),每個上報人上報錯誤點的總數(shù)以及已解決錯誤的總數(shù),閑話不說,看代碼
有一張錯誤上報表,下面只將與本文相關(guān)的字段羅列如下:上報人(ReportPerson)、上報錯誤ID(ErrorID)、上報時間(ReportTime)、狀態(tài)(State),其中值為0(未解決)、1(已處理)、2(已解決)。
現(xiàn)在要做的是統(tǒng)計在某個時間段[beginTime,endTime](其中beginTime,endTime由前臺進行傳入)內(nèi),每個上報人上報錯誤點的總數(shù)以及已解決錯誤的總數(shù)。
select a.ReportPerson,a.sumOfError,b.solvedError
from(select COUNT(ErrorID) as sumOfError,ReportPerson
from PCR_ConstructInfo
where
(ReportTime>beginTime) and (ReportTime<endTime) group by ReportPerson)
a left join
(select ReportPerson,COUNT(ErrorID) as solvedError
from PCR_ConstructInfo
where (State=2) and (ReportTime>beginTime) and (ReportTime<endTime) group by ReportPerson) b
on (a.ReportPerson=b.ReportPerson)
生成的結(jié)果圖為:
現(xiàn)在要做的是統(tǒng)計在某個時間段[beginTime,endTime](其中beginTime,endTime由前臺進行傳入)內(nèi),每個上報人上報錯誤點的總數(shù)以及已解決錯誤的總數(shù)。
復(fù)制代碼 代碼如下:
select a.ReportPerson,a.sumOfError,b.solvedError
from(select COUNT(ErrorID) as sumOfError,ReportPerson
from PCR_ConstructInfo
where
(ReportTime>beginTime) and (ReportTime<endTime) group by ReportPerson)
a left join
(select ReportPerson,COUNT(ErrorID) as solvedError
from PCR_ConstructInfo
where (State=2) and (ReportTime>beginTime) and (ReportTime<endTime) group by ReportPerson) b
on (a.ReportPerson=b.ReportPerson)
生成的結(jié)果圖為:

相關(guān)文章
sqlserver CONVERT()函數(shù)用法小結(jié)
文章分析總結(jié)了關(guān)于CONVERT()函數(shù)在操作日期時的一些常見的用法分析下面來看看2012-09-09insert into tbl() select * from tb2中加入多個條件
insert into tbl() select * from tb2中加入多個條件2009-06-06數(shù)據(jù)庫更新Sqlserver腳本總結(jié)
數(shù)據(jù)庫更新Sqlserver腳本總結(jié),需要的朋友可以參考下。2011-06-06sql分組后二次匯總(處理表重復(fù)記錄查詢和刪除)的實現(xiàn)方法
這篇文章主要介紹了sql分組后二次匯總的實現(xiàn)方法,需要的朋友可以參考下2017-02-02SQLSERVER分頁查詢關(guān)于使用Top方式和row_number()解析函數(shù)的不同
這篇文章主要介紹了SQLSERVER分頁查詢關(guān)于使用Top方式和row_number()解析函數(shù)的不同的相關(guān)資料,需要的朋友可以參考下2016-02-02SQL order by ID desc/asc加一個排序的字段解決查詢慢問題
解決方法就是在order by ID desc再加一個排序的字段,這樣子可能會把速度提高很多,需要朋友可以試一下2012-12-12