从结果集中计算重复值

时间:2017-11-07 00:58:57

标签: sql sql-server sql-server-2008

希望显示结果集中特定重复值的计数。

以下是其他几个SP在

下面调用的存储过程[dbo].[SP_Query_Weekly]
(
  @StartReportDate datetime 
, @EndReportDate   datetime 
, @DeptCode        char(2) 
, @OpCode          char(2)
, @Goal            decimal(10,2)
) as 
begin;
if exists (
select 1 
from TimeLog t
where t.EventDate >= @StartReportDate
  and t.EventDate <= @EndReportDate
  and t.DeptCode   = @DeptCode
  and t.OpCode     = @OpCode
)
begin;
declare @Header   varchar(100)
set     @Header = 'ID      Codes   TimeSUM   Units    UPH  Goal%   AssociateName';
select  ' '
select  @Header
select  '--      -----   -------   -----    ---  -----   -------------';
select 
    ID            = right(space(7)+rtrim(t.ID),7)  
  , Codes         = (t.DeptCode + '-' +  t.OpCode)
  , TimeSUM       = right(space(5)+rtrim(right(convert(varchar(9),(sum(datediff(second,StartTime,FinishTime)) / 3600 )),3) + ':' 
                  + right('0' + convert(varchar(2),(sum(datediff(second,StartTime,FinishTime)) / 60) % 60 ),2)),5) 
  , Units         = right(space(7)+rtrim(PARSENAME(convert(varchar,cast(sum(units) as money),1),2)),7)
  , UPH           = right(space(6)+rtrim(cast(isnull(sum(Units) / nullif(sum(datediff(minute,StartTime,FinishTime))*1.0,0),0.0)*60  as decimal(10,0))),6)
  , [Goal%]       = right(space(5)+rtrim(isnull((convert(varchar(30),cast((isnull(sum(Units) / nullif(sum(datediff(minute,StartTime,FinishTime))*1.0,0),0.0)*60)/nullif(@Goal,0)*100 as decimal(10,0))) + '%'), ' ')),5)
  , AssociateName = UPPER(isnull(ai.FirstName + ' ','************************fix me') + isnull(ai.LastName,''))
from TimeLog t
  left join AssociateInfo ai
    on t.Id = ai.Id
where t.EventDate >= @StartReportDate
  and t.EventDate <= @EndReportDate
  and t.DeptCode = @DeptCode
  and t.OpCode = @OpCode
group by t.id, t.DeptCode, t.OpCode, ai.FirstName, ai.LastName
order by UPH desc;
end;
end;

以下是调用上述()的其他过程[dbo].[SP_FLAT_Weekly]之一,这里实际上有几个select语句,我只显示2来尝试简化我的请求...

begin;
set nocount on; --/* removes (N row(s) affected message)
declare 
  @Now             datetime
, @StartReportDate datetime 
, @EndReportDate   datetime 
, @DeptCode        char(2) 
, @OpCode          char(2)
, @Goal            decimal(10,2)    
set @StartReportDate = dateadd(day, datediff(day, 0, getdate()), -8)
set @EndReportDate   = dateadd(day, datediff(day, 0, getdate()), -2);
------------------------------------------THIS IS FOR DEPARTMENT CODE [FS - FD]
select 
  @Now = getdate()
, @StartReportDate = @StartReportDate
, @EndReportDate   = @EndReportDate
, @DeptCode = 'FS'
, @OpCode   = 'FD'
, @Goal     = 0.0;
exec dbo.SP_Query_Weekly @StartReportDate, @EndReportDate, @DeptCode, @OpCode, @Goal; 
------------------------------------------THIS IS FOR DEPARTMENT CODE [FS - FE]
select 
  @Now = getdate()
, @StartReportDate = @StartReportDate
, @EndReportDate   = @EndReportDate
, @DeptCode = 'FS'
, @OpCode   = 'FE'
, @Goal     = 0.0;
exec dbo.SP_Query_Weekly @StartReportDate, @EndReportDate, @DeptCode, @OpCode, @Goal; 
end;

这是我得到的结果集

ID      Codes   TimeSUM   Units    UPH  Goal%   AssociateName
--      -----   -------   -----    ---  -----   -------------
2372682 FS-FD   15:46    22,601   1433          ************************FIX ME
  81423 FS-FD    2:15     3,173   1410          DIFFERENT NAME1
2372126 FS-FD   14:42    20,655   1405          DIFFERENT NAME2
2375270 FS-FD    4:10     5,599   1344          ************************FIX ME
2371738 FS-FD   17:01    22,509   1323          DIFFERENT NAME3
  95362 FS-FD   10:23    12,054   1161          DIFFERENT NAME4
2563306 FS-FD    5:50     5,273    904          DIFFERENT NAME5
2626999 FS-FD    5:20     4,404    826          ************************FIX ME
2427699 FS-FD    0:05         0      0          DIFFERENT NAME6

ID      Codes   TimeSUM   Units    UPH  Goal%   AssociateName
--      -----   -------   -----    ---  -----   -------------
2372682 FS-FE   15:46    22,601   1433          ************************FIX ME
  81423 FS-FE    2:15     3,173   1410          DIFFERENT NAME1
2372126 FS-FE   14:42    20,655   1405          DIFFERENT NAME2
2375270 FS-FE    4:10     5,599   1344          ************************FIX ME
2371738 FS-FE   17:01    22,509   1323          DIFFERENT NAME3
  95362 FS-FE   10:23    12,054   1161          DIFFERENT NAME4
2563306 FS-FE    5:50     5,273    904          DIFFERENT NAME5
2626999 FS-FE    5:20     4,404    826          ************************FIX ME
2427699 FS-FE    0:05         0      0          DIFFERENT NAME6

ErrorCount = 6

在此结果集的底部,我希望看到ErrorCount列显示显示************************FIX ME的所有行的总计数

0 个答案:

没有答案