gpt4 book ai didi

sql - 如果基于另一列中的值求和

转载 作者:行者123 更新时间:2023-12-04 19:04:09 25 4
gpt4 key购买 nike

在下面的代码中,我希望AdminDurationBreakDuration基于UserID的值求和。目前,我无法弄清楚,它会将所有Duration的值与ReasonCode 7求和。结果,每个UserId都具有相同的Duration(不是我想要的!)。

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

最佳答案

如果我理解正确,应该是:

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

关于sql - 如果基于另一列中的值求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14631625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com