需要只为每个代理名称显示一行(SQL)

时间:2016-05-31 09:02:45

标签: sql tsql

这个论坛的新手很容易,我在这里迷路了,只想在每个名字上显示一行,即Joe Blogs ....任何人都可以看到我出错了,因为目前每个名字都有多行。 / p>

DECLARE @StartDate DATETIME, @EndDate DATETIME
SELECT @StartDate = '2016-05-31 00:00:01', @EndDate = '2016-05-31 23:00:00'

SELECT      CONVERT(Varchar, AgentSession.SessionStart, 103) AS Date, 
            Agent.Firstname + ' ' + Agent.Lastname AS AgentName, 
            ProductivitySummary.SessionStartTime As FirstLogin,
            ProductivitySummary.SessionEndTime As LastLogin,
            ProductivitySummary.PLogOnTime,
            (AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) As LogOnTime,
            (AgentSession.TalkTime + AgentSession.HoldTime) As TalkTime, 
            (AgentSession.WaitTime + AgentSession.PreviewTime) As WaitTime,
            (AgentSession.WrapTime + AgentSession.DeadTime) As WrapTime, 
            AgentSession.BreakTime,
            AgentSession.MealTime,
            AgentSession.MeetingTime,
            AgentSession.OtherTime
FROM        Agent INNER JOIN
                      AgentSession ON Agent.AgentID = AgentSession.AgentID
            INNER JOIN(
SELECT      CONVERT(Varchar, AgentSession.SessionStart, 103) AS Date, 
            Agent.AgentID,
            Agent.Firstname + ' ' + Agent.Lastname AS AgentName,
            MIN(AgentSession.SessionStart) SessionStart, 
            MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8)) AS SessionStartTime,
            MAX(AgentSession.SessionEnd) SessionEnd, 
            MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)) AS SessionEndTime,
            CASE WHEN SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) > DATEDIFF(ss, (MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8))), (MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)))) THEN SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) ELSE DATEDIFF(ss, (MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8))), (MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)))) END AS PLogOnTime,
            SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) As LogOnTime,
            COUNT(AgentSession.AgentID) Logouts,
            SUM(AgentSession.TalkTime + AgentSession.HoldTime) As TalkTime, 
            SUM(AgentSession.WaitTime + AgentSession.PreviewTime) As WaitTime,
            SUM(AgentSession.WrapTime + AgentSession.DeadTime) As WrapTime, 
            SUM(AgentSession.BreakTime) As BreakTime,
            SUM(AgentSession.MealTime) As MealTime,
            SUM(AgentSession.MeetingTime) As MeetingTime,
            SUM(AgentSession.OtherTime) As OtherTime
FROM        Agent INNER JOIN
                      AgentSession ON Agent.AgentID = AgentSession.AgentID
WHERE       (AgentSession.SessionStart Between @StartDate AND @EndDate) AND (Agent.Lastname Not Like '%(DND)')
GROUP BY    CONVERT(Varchar, AgentSession.SessionStart, 103), Agent.AgentID, Agent.Firstname + ' ' + Agent.Lastname
)ProductivitySummary
ON CONVERT(Varchar, AgentSession.SessionStart, 103) = ProductivitySummary.Date AND AgentSession.AgentID = ProductivitySummary.AgentID

WHERE       (AgentSession.SessionStart Between @StartDate AND @EndDate) AND (Agent.Lastname Not Like '%(DND)')
ORDER BY    Agent.Firstname + ' ' + Agent.Lastname ASC, CONVERT(CHAR(8), AgentSession.SessionStart, 8)     

1 个答案:

答案 0 :(得分:0)

试试这个..我刚刚从group by子句中删除了“AgentSession.SessionStart”。似乎代理可以有多个会话,你需要Min的那个,所以不应该在Group By ... Rest你可以尝试这个..让我知道如果仍然有多个

DECLARE @StartDate DATETIME, @EndDate DATETIME
SELECT @StartDate = '2016-05-31 00:00:01', @EndDate = '2016-05-31 23:00:00'

SELECT      CONVERT(Varchar, AgentSession.SessionStart, 103) AS Date, 
            Agent.Firstname + ' ' + Agent.Lastname AS AgentName, 
            ProductivitySummary.SessionStartTime As FirstLogin,
            ProductivitySummary.SessionEndTime As LastLogin,
            ProductivitySummary.PLogOnTime,
            (AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) As LogOnTime,
            (AgentSession.TalkTime + AgentSession.HoldTime) As TalkTime, 
            (AgentSession.WaitTime + AgentSession.PreviewTime) As WaitTime,
            (AgentSession.WrapTime + AgentSession.DeadTime) As WrapTime, 
            AgentSession.BreakTime,
            AgentSession.MealTime,
            AgentSession.MeetingTime,
            AgentSession.OtherTime
FROM        Agent INNER JOIN
                      AgentSession ON Agent.AgentID = AgentSession.AgentID
            INNER JOIN(
SELECT      CONVERT(Varchar, MIN(AgentSession.SessionStart), 103) AS Date, 
            Agent.AgentID,
            Agent.Firstname + ' ' + Agent.Lastname AS AgentName,
            MIN(AgentSession.SessionStart) SessionStart, 
            MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8)) AS SessionStartTime,
            MAX(AgentSession.SessionEnd) SessionEnd, 
            MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)) AS SessionEndTime,
            CASE WHEN SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) > DATEDIFF(ss, (MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8))), (MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)))) THEN SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) ELSE DATEDIFF(ss, (MIN(CONVERT(CHAR(8), AgentSession.SessionStart, 8))), (MAX(CONVERT(CHAR(8), AgentSession.SessionEnd, 8)))) END AS PLogOnTime,
            SUM(AgentSession.TalkTime + AgentSession.HoldTime + AgentSession.WaitTime + AgentSession.PreviewTime + AgentSession.WrapTime + AgentSession.DeadTime + AgentSession.BreakTime + AgentSession.MealTime + AgentSession.MeetingTime + AgentSession.OtherTime) As LogOnTime,
            COUNT(AgentSession.AgentID) Logouts,
            SUM(AgentSession.TalkTime + AgentSession.HoldTime) As TalkTime, 
            SUM(AgentSession.WaitTime + AgentSession.PreviewTime) As WaitTime,
            SUM(AgentSession.WrapTime + AgentSession.DeadTime) As WrapTime, 
            SUM(AgentSession.BreakTime) As BreakTime,
            SUM(AgentSession.MealTime) As MealTime,
            SUM(AgentSession.MeetingTime) As MeetingTime,
            SUM(AgentSession.OtherTime) As OtherTime
FROM        Agent INNER JOIN
                      AgentSession ON Agent.AgentID = AgentSession.AgentID
WHERE       (AgentSession.SessionStart Between @StartDate AND @EndDate) AND (Agent.Lastname Not Like '%(DND)')
GROUP BY     Agent.AgentID, Agent.Firstname + ' ' + Agent.Lastname
)ProductivitySummary
ON CONVERT(Varchar, AgentSession.SessionStart, 103) = ProductivitySummary.Date AND AgentSession.AgentID = ProductivitySummary.AgentID

WHERE       (AgentSession.SessionStart Between @StartDate AND @EndDate) AND (Agent.Lastname Not Like '%(DND)')
ORDER BY    Agent.Firstname + ' ' + Agent.Lastname ASC, CONVERT(CHAR(8), AgentSession.SessionStart, 8)