我在这里发现了类似但不太匹配的场景,并且没有任何运气让它起作用。如果这是一个重复的问题,请原谅我,我肯定是先搜索的!我有三张 table 。第一个 A 是帮助台工单的列表。第二个,B,时间条目列表。第三个,C,关于第二个中时间条目的更多细节。对于 A 中的每个项目,B 中可以有多个项目,但是对于 B 中的每个项目,C 中只有一个条目。
wh_task 作为 A
task_id create_time source_id
======== ============ ==========
1351000 2013-01-23 12:03:23.590 8
1351001 2013-01-23 13:03:23.590 5
1351002 2013-01-23 15:03:23.590 8
wh_time_item 作为 B
task_id time_item_id created_by_user_id
======== ============ =================
1351000 2456 1234567
1351000 2457 2345786
1351000 2458 1234567
wh_time_subitem 作为 C
time_item_id create_time
======== ============
2456 2013-01-23 12:43:23.590
2457 2013-01-25 13:13:23.590
2458 2013-02-12 16:03:23.590
高层次目标 -
在给定的日期范围内,按工程师确定每个工单的平均 FIRST 响应时间。
具体来说 -
首先,查找 A 中在 @StartDate 和 @EndDate 之间创建的所有项目。接下来,找到 A 中 source_id = 8 的所有项目(这些是我唯一关心的票)。然后,我需要找到 B 中的哪个项目是“第一个”条目,即最接近 A 中项目创建的日期。尽管表 B 没有创建日期 - 在 C 中。
一旦我确定了 B 中的“第一个”项目,我需要查看 created_by_user_id 是否与 @Engineer 匹配。最后,我想要所有匹配项的 a.create_time 和 c.create_time 之间的日期差异的平均值,以分钟为单位。类似于 AVERAGE(DATEDIFF(MI, a.create_time, c.createtime) 作为 ResponseTime。
在过去的两天里,我已经进行了十多次迭代,这是我现在的错误查询。我知道这个查询,即使它可以运行,也不会给我我想要的 - 一直在摆弄它以提取额外的列以进行故障排除:
DECLARE @StartDate datetime
DECLARE @EndDate datetime
DECLARE @Engineer integer
SET @StartDate = '04/01/13'
SET @EndDate = '04/30/13 23:59:59'
SET @StartDate = DATEADD(S, (DateDiff(s, getdate(), getutcdate())), @StartDate)
SET @EndDate = DATEADD(S, (DateDiff(s, getdate(), getutcdate())), @EndDate)
SET @Engineer = 1234567
SELECT [task_number]
,a.[create_time]
,DateDiff(MI, a.create_time, c.create_time) as a_responsetime
,b.user_id
,c.[time_subitem_id]
,a.ticket_source_id
FROM [databasename].[dbo].[wh_task] as a
LEFT JOIN [databasename].[dbo].[wh_time_item] AS b
ON a.task_id = b.task_id
LEFT JOIN [[databasename].[dbo].[wh_time_subitem] AS c
ON b.time_item_id = (SELECT c.time_item_id from [databasename].[dbo].[wh_time_subitem] WHERE c.create_time = (SELECT MIN(c.create_time) from [databasename].[dbo].[wh_time_subitem]))
WHERE
b.user_id = @Engineer
AND a.ticket_source_id = 8
AND c.create_time between @StartDate and @EndDate
ORDER BY a.ticket_source_id
在此先感谢您提供的任何帮助。我充其量只是一个 SQL 爱好者,所以不要担心伤害我的感情。 :-)
最佳答案
Jazz,跟进我发送给您的电子邮件,对您提供的示例数据使用以下内容,我得到了我认为您正在寻找的正确数据点:
declare @StartDate datetime
declare @EndDate datetime
declare @Engineer int
SET @StartDate = '01/01/13'
SET @EndDate = '01/30/13 23:59:59'
SET @StartDate = DATEADD(S, (DateDiff(s, getdate(), getutcdate())), @StartDate)
SET @EndDate = DATEADD(S, (DateDiff(s, getdate(), getutcdate())), @EndDate)
SET @Engineer = 1234567
select AVG(first_response_time) from ( select A.*, DATEDIFF(minute, A.create_time, (
select top 1 C.create_time
from wh_time_item as B
inner join wh_time_subitem as C on C.time_item_id = B.time_item_id
order by C.create_time asc ) ) as first_response_time
from wh_task as A
inner join wh_time_item as B on B.task_id = A.task_id
where A.source_id = 8
and A.create_time between @StartDate and @EndDate
and B.created_by_user_id = @Engineer
) as first_response_table
请注意,针对 wh_time_item 和 wh_time_subitem 的子查询假定 create_time 将始终在 wh_task create_time 上或之后...这似乎是一个可靠的假设。
关于sql-server-2008 - MS SQL 2008,连接 3 个表,按 T1 和 T2 中的列过滤,仅查找 T3 中最小值的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740535/