尝试在一年内排除2天

时间:2016-12-12 21:01:19

标签: sql sql-server

我在查询中添加了日期,因此您可以查看我正在运行的日期。从这些日期开始,我试图排除2天10/6/2016和2016年10月7日

DECLARE @Startdate AS DATETIME
DECLARE @EndDate AS DATETIME
SET @Startdate = '10/1/2015'
SET @EndDate = '9/30/2016

SELECT     A.agent_name, COUNT(*) AS CH, (CAST(SUM(reporting_call_matrix.talk_time) / COUNT(reporting_call_matrix.answer_time) AS float) 
                  + CAST(SUM(reporting_call_matrix.hold_time) AS float) / COUNT(reporting_call_matrix.answer_time)) + 
                  CAST(SUM(reporting_call_matrix.work_time) AS float) / COUNT(reporting_call_matrix.answer_time) AS AHT, 
                  answer_agent_id
FROM         reporting_call_matrix  INNER JOIN
                  reporting_agents AS A  ON reporting_call_matrix.answer_agent_id = A.agent_id INNER JOIN
                  reporting_split_info ON reporting_call_matrix.split = reporting_split_info.split 
WHERE     (reporting_call_matrix.answer_agent_id IS NOT NULL) AND (reporting_call_matrix.split IN (9,23)) AND 
                  (reporting_call_matrix.queued_time >= @StartDate) AND (reporting_call_matrix.queued_time < DATEADD(d, 1, @EndDate)) AND 
                  (reporting_call_matrix.answer_time IS NOT NULL) AND 
GROUP BY  A.agent_name,answer_agent_id

1 个答案:

答案 0 :(得分:0)

不知道你在这里尝试做什么,但我清理了这个查询,因此它更清晰。我也大大简化了噩梦般的计算。您不必将所有内容都转换为浮点数,只需将其多出1.0即可。另外,如果你想要精确度,float可能不是一个很好的选择,因为它是一个近似的数据类型,其中1.0将是数字,这是一个确切的数据类型。

使用一些别名和格式,这样更容易阅读。

SELECT A.agent_name
    , COUNT(*) AS CH
    , (SUM(rcm.talk_time) + SUM(rcm.hold_time) / COUNT(rcm.answer_time) * 1.0) + (SUM(rcm.work_time) / COUNT(rcm.answer_time) * 1.0) AS AHT
    , answer_agent_id
FROM reporting_call_matrix  rcm 
INNER JOIN reporting_agents AS A  ON rcm.answer_agent_id = A.agent_id 
INNER JOIN reporting_split_info rsi ON rcm.split = rsi.split 
WHERE rcm.answer_agent_id IS NOT NULL
    AND rcm.split IN (9,23)
    AND rcm.queued_time >= @StartDate
    AND rcm.queued_time < DATEADD(day, 1, @EndDate)
    AND rcm.answer_time IS NOT NULL
GROUP BY A.agent_name
    , rcm.answer_agent_id

现在,这里的实际问题是什么?

根据您最近的更新,我会猜测queued_time是一个日期数据类型吗?

为什么不简单地在where子句中添加一个谓词?

AND rcm.queued_time not in ('2016-10-06', '2016-10-07')