2013-01-31 3 views
2

아래 코드에서 UserID 값이 무엇인지에 따라 AdminDuration 및 BreakDuration을 합산합니다. 지금은 알아낼 수 없으며 모든 기간의 값을 ReasonCode 7로 합산합니다. 결과적으로 모든 UserId는 동일한 기간을 보냅니다. 내가 제대로 이해하면다른 열의 값을 기준으로 합계

Select SkillTargetID AS UserID, 
(SELECT sum(Duration) 
    from [t_Agent_Event_Detail] 
    where ReasonCode = 7 
    and DateTime > convert(DATETIME, '2013-01-31 08:00', 21)) as AdminDuration, 
(SELECT sum(Duration) 
    from [t_Agent_Event_Detail] 
    where ReasonCode = 6 
    and DateTime > convert(DATETIME, '2013-01-31 08:00', 21) 
    and SkillTargetID = [t_Agent_Event_Detail].SkillTargetID) as BreakDuration 
from [t_Agent_Event_Detail] 
GROUP BY SkillTargetID 

답변

8

, 그것은해야한다 :

SELECT SkillTargetID AS UserID, 
     sum(CASE WHEN ReasonCode = 7 THEN Duration ELSE 0 END) as AdminDuration, 
     sum(CASE WHEN ReasonCode = 6 THEN Duration ELSE 0 END) as BreakDuration 
FROM [t_Agent_Event_Detail] 
WHERE DateTime > convert(DATETIME, '2013-01-31 08:00', 21) 
GROUP BY SkillTargetID 
관련 문제