合并到存储过程,一对多

时间:2013-09-19 15:04:49

标签: sql

所以我不确定我想做什么但是这里...我有两个存储过程...一个计算一个人每天必须工作的空闲时间并将记录集放在一个临时值表格为速度...结果为as,字段为Date,PersonID,Hours,Minutes。

2013-04-03  10000305    8   30
2013-05-03  10000305    8   30

在他们的工作日内他们完成通话,每个通话都有记录的持续时间。我的第二个SP计算出他们当天实际工作的时间。结果如下,字段相同,日期,人员,小时,分钟。

2013-04-25  10000305    7   20
2013-04-26  10000305    11  7

问题:我必须打两个SP然后组织。我希望的是我可以将所有内容放入一个SP中,并且只是有一个如下所示的摘要。问题是第二个SP在一天内计算了许多记录以获得其总和,而第一个SP只是一天和一个持续时间。当我试图加在一起时,我的结果变得疯狂并买回了数百万条记录。有简单的方法还是不可能?

**Date**    **User**   **Hours**    **Minutes**  **Actual Hours**   **Act Mins**
2013-04-25  10000305    8            30           11                    7

这是一段代码片段,两者的工作方式非常相似

CREATE TABLE #TotalUsedTime 
  (PersonID Int, 
  [Date] DateTime,
  Office Int, 
  [Hours] Int, 
  [Minutes] Int)

    DECLARE @GetUsedTime NVARCHAR(MAX);
        SET @GetUsedTime = N'
Select  PersonID, [Date], Office, TotalUsedTime / 3600 AS Hours, (TotalUsedTime % 3600) / 60 AS Minutes
     From 
( Select  PersonID, [Date], Office, DateDiff(Second, Starttime, End) AS TotalUsedTime From Calls Where Completed IS NOT NULL)  x '

        INSERT #TotalUsedTime EXEC SP_Executesql @GetUsedTime;

        Select Cast([Date] as date) Date, #TotalUsedTime.PersonID,
        Cast(sum([hours] * 60 + [minutes])/60 as int) As Hours,
        Sum([hours] * 60 + [minutes]) - 60 * cast(sum([hours] * 60 + [minutes])/60 as int) As Minutes,
        People.Fullname
        From #TotalUsedTime
        Inner Join People on People.PersonID = #TotalUsedTime.PersonID
        Where #TotalUsedTime.[Date] Between @StartDate and @EndDate and Office = @Office
        and People.PersonID = @PersonID 
        Group by cast([DATE] as Date), #TotalUsedTime.PersonID, People.FullName]

        End

0 个答案:

没有答案