1:1结果的关系/范围内的日期

时间:2013-01-14 23:09:11

标签: sql sql-server resultset

我正在尝试让我的查询输出我所拉动范围内每一天的一行效果结果。我每天每个代表大约持续10行,结果看起来很奇怪。您是否能够验证我如何改变我的查询以将结果分解为一天一天的阅读?对不起有问题...让我知道我是否应该用不同的方式说出来。

以下是代码:

DECLARE @StartDate AS SMALLDATETIME
DECLARE @EndDate AS SMALLDATETIME

SET @StartDate = '2012-04-01'
SET @EndDate = '2012-04-30'

SET ANSI_WARNINGS OFF

SELECT
      DT.DateFull,
      --DATEPART(MM,DT.DateFull) AS Date,
      --DATENAME(MM,DT.DateFull) AS [Month],  
      E.AreaID,      
      --E.GeographicLocationID,      
      E.EmployeeID,
      ES.PeopleSoftTitleStartDate,
      QA.TopRep,
      QA.RepCount,
      CASE 
          WHEN SUM(QA.TopRep) = '0' OR SUM(RepCount) = '0'
          THEN '0'
          ELSE CAST(100*(SUM(QA.TopRep))/SUM(QA.RepCount) AS DECIMAL (8,2))
      END 'ERP',  
      QA.RepResolve,
      QA.RepResolveCount,
      CASE 
          WHEN SUM(QA.RepResolve) = '0' OR SUM(RepResolveCount) = '0'
          THEN '0'
          ELSE CAST(100*(SUM(QA.RepResolve))/SUM(QA.RepResolveCount) AS DECIMAL (8,2))
      END 'RR',
      QA.ACSNPSPromoter,
      QA.ACSNPSDetractor,
      QA.ACSNPSCount,
      CASE
          WHEN QA.ACSNPSCount = '0'
          THEN '0'
          ELSE CAST(100*(SUM(QA.ACSNPSPromoter)-SUM(QA.ACSNPSDetractor))/SUM(QA.ACSNPSCount) AS DECIMAL(8,2))
      END 'NPS',
      AHT.AHT,
      AHT.TalkTime,
      AHT.CallWorkTime,
      AHT.HandledCalls/*,
      CASE  
        WHEN (DT.DateFull Between emp.Transition_Date and emp.Month_one) 
        THEN 'First 30 Days'
        WHEN (DT.DateFull Between emp.Month_one And emp.Month_Two ) 
        THEN '30 - 60 days'
        WHEN (DT.DateFull Between emp.Month_Two And emp.Month_Three) 
        THEN '60-90 days'
        ELSE 'After Period'
      END AS Stat_Period*/

INTO #DATA              

FROM 
    dbTools.Config.vwDate AS DT WITH(NOLOCK)     

INNER JOIN 
     dbEmployee.Summary.vwEmployeeHistory AS E WITH(NOLOCK)          

ON 
    DT.datefull BETWEEN E.StartDate AND E.EndDate

LEFT JOIN
    dbEmployee.Summary.vwEmployeeSnapshot AS ES WITH(NOLOCK)
ON
    E.EmployeeID = ES.EmployeeID
    AND DT.datefull BETWEEN E.StartDate AND E.EndDate

LEFT JOIN
--ERP and Rep Resolve
(SELECT
    MONTH(StatDate) AS Mth,
    EmployeeID,
    --SUM(ACSTopRepScore)/SUM(ACSEntireRepCount) AS ERP,
    ACSTopRepScore AS TopRep,
    ACSEntireRepCount AS RepCount,
    --SUM(ACSRepResolvedScore)/SUM(ACSRepResolvedCount) AS RepResolve,
    ACSRepResolvedScore AS RepResolve,
    ACSRepResolvedCount AS RepResolveCount,
    ACSNPSPromoter AS ACSNPSPromoter,
    ACSNPSDetractor AS ACSNPSDetractor,
    ACSNPSCount AS ACSNPSCount

FROM
    dbReportSummary.ReportSummary.vwAfterCallSurvey WITH(NOLOCK)

WHERE
    StatDate BETWEEN @StartDate AND @EndDate

GROUP BY
    MONTH(StatDate),
    EmployeeID,
    ACSTopRepScore,
    ACSEntireRepCount, 
    ACSRepResolvedScore, 
    ACSRepResolvedCount,
    ACSNPSPromoter, 
    ACSNPSDetractor,
    ACSNPSCount



) AS QA

ON
    (QA.Mth IN (MONTH(DT.DateFull)))
    AND QA.EmployeeID = E.EmployeeID

LEFT JOIN
--AHT--
(SELECT 
    MONTH(StatDate) AS Mth,
    EmployeeId,
    HandledCalls AS HandledCalls,
    TalkTime AS TalkTime,
    CallWorkTime AS CallWorkTime,
    CASE
        WHEN SUM(TalkTime) + SUM(CallWorkTime) = 0 OR SUM(HandledCalls) = 0
        THEN 0.0
            ELSE (SUM(TalkTime) + SUM(CallWorkTIme))/SUM(HandledCalls)
        END 'AHT'

FROM
    dbReportSummary.ReportSummary.vwACDAgentSummary WITH(NOLOCK)

WHERE
    StatDate BETWEEN @StartDate AND @EndDate

GROUP BY
    MONTH(StatDate),
    EmployeeId,
    HandledCalls, 
    TalkTime,
    CallWorkTime

) AS AHT

ON
    (AHT.Mth IN (MONTH(DT.DateFull)))
    AND AHT.EmployeeID = E.EmployeeID

WHERE
    (DT.DateFull <=@EndDate AND DT.DateFull >=@StartDate)
    AND (E.StartDate <=@EndDate AND E.EndDate >=@StartDate)
    AND (E.PeopleSoftDepartmentFunctionID = 'CSC')
    AND E.PeopleSoftDepartmentID IN ('9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>', '<<OSVTS_TCS>>','9085')
    AND E.EmployeeID IS NOT NULL
    AND (ES.PeopleSoftTitleStartDate BETWEEN DT.DateFull AND DT.DateFull + 90)

GROUP BY
        DT.DateFull,
      --DATEPART(MM,DT.DateFull),
      --DATENAME(MM,DT.DateFull), 
      E.AreaID,                                
     -- E.GeographicLocationID,       
      E.EmployeeID,
      ES.PeopleSoftTitleStartDate,
      QA.TopRep,
      QA.RepCount,
      QA.RepResolve,
      QA.RepResolveCount,
      QA.ACSNPSPromoter,
      QA.ACSNPSDetractor,
      QA.ACSNPSCount,
      AHT.AHT,
      AHT.TalkTime,
      AHT.CallWorkTime,
      AHT.HandledCalls         

SELECT
    D.DateFull,
    --Date,
    --[Month],
    D.AreaID,
    --GE.GeographicLocationDescription AS Center,           
    D.EmployeeID,
    nE.EmployeeName,
    D.PeopleSoftTitleStartDate,     
    D.TopRep,
    D.RepCount,
    D.ERP,
    D.RepResolve,
    D.RepResolveCount,
    D.RR,
    D.ACSNPSPromoter,
    D.ACSNPSDetractor,
    D.ACSNPSCount,
    D.NPS,
    D.AHT,
    D.TalkTime,
    D.CallWorkTime,
    D.HandledCalls

FROM #DATA AS D                                                        

LEFT JOIN 
    dbEmployee.Config.vwName AS nE WITH (NOLOCK)   
ON
    D.EmployeeID = nE.EmployeeID

--LEFT JOIN 
--  dbEmployee.Config.vwGeographicLocation AS GE WITH (NOLOCK)               
--ON 
--  GE.GeographicLocationID = D.GeographicLocationID              

WHERE
    (D.TopRep IS NOT NULL
    AND D.RepCount IS NOT NULL
    AND D.RepResolve IS NOT NULL
    AND D.RepResolveCount IS NOT NULL
    AND D.ACSNPSPromoter IS NOT NULL
    AND D.ACSNPSDetractor IS NOT NULL
    AND D.ACSNPSCount IS NOT NULL
    AND D.AHT IS NOT NULL
    AND D.TalkTime IS NOT NULL
    AND D.CallWorkTime IS NOT NULL
    AND D.HandledCalls IS NOT NULL
    AND D.HandledCalls <> '0' )


GROUP BY
        D.DateFull,
      --Date,
      --[Month],    
      D.AreaID,               
      --GE.GeographicLocationDescription,           
      D.EmployeeID,
      nE.EmployeeName,
    D.PeopleSoftTitleStartDate,
      D.TopRep,
      D.RepCount,
      D.ERP,
      D.RepResolve,
      D.RepResolveCount,
      D.RR,
      D.ACSNPSPromoter,
      D.ACSNPSDetractor,
      D.ACSNPSCount,
      D.NPS,
      D.AHT,
      D.TalkTime,
      D.CallWorkTime,
      D.HandledCalls

ORDER BY 
      D.DateFull ASC,
      AreaID,                   
 --     Center,           
      EmployeeName    

DROP TABLE #DATA

/*Additional Info
Tech Depts - PeopleSoftDepartmentID IN ('9030', '9080', '9355', '9040', '<<OSCCALTECH>>', '<<WDTSMO>>', '9195','<<OSVTS>>', '<<OSVTS_TCS>>')
Bilingual Tech - PeopleSoftDepartmentID = '9085'
*/

1 个答案:

答案 0 :(得分:1)

  

每天输出一行效果结果

每天一行输出意味着group by [Date]group by子句中的其他列也会导致服务器按其分组,可能会创建多行/天。不过,我猜测,看看你的查询,就是你每天对每一行感兴趣(你说“代表”)。在这种情况下,您也希望group by员工。

如果您希望结果中包含其他非聚合列,请稍后加入。这是一个草图:

select A.*, E.name, ... E.col_N
from ( 
  select [Date], EmployeeID, max(foo) as x, sum(bar) as y, avg(zowie) as z
  from tablename 
  group by [Date], EmployeeID
) as A
join Employees as E
on A.EmployeeID = E.EmployeeID