根据时分秒统计指定数据
select a.CreateDate,convert(varchar,datepart(year,a.CreateDate)) Year,convert(varchar,datepart(month,a.CreateDate)) Month, convert(varchar,datepart(day,a.CreateDate)) Day,convert(varchar,datepart(hour,a.CreateDate)) Hour , {branchNoStr} ISNULL(PayCount,0) NumResult, ISNULL(b.CustID,@custid) CustId, @ModuleType ModuleType from ( SELECT substring(convert(char(32),DATEADD(HH,number,@beginDT),120),1,16) AS CreateDate ,type FROM master..spt_values WHERE type = 'p' AND DATEDIFF(HH,DATEADD(HH,number,@beginDT),@endDT)>=0 ) a left join ( select substring(convert(char(32),DATEADD(HH,datepart(hh,CreateDate),convert(char(32),CreateDate,23)),120),1,16) as st, Count(CustId) PayCount,BranchNo,CustId from OrderMaster where PayFlag = 1 and CustId = @custid {whereStr} and CreateDate between @beginDT and @endDT group by convert(char(32),CreateDate,23),datepart(hh,CreateDate),BranchNo,CustId) b on b.st=a.CreateDate order by CreateDate