Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 MS SQL 2008,联接3个表,按T1和T2中的列进行筛选,只查找T3中最小值的平均值_Sql Server 2008 - Fatal编程技术网

Sql server 2008 MS SQL 2008,联接3个表,按T1和T2中的列进行筛选,只查找T3中最小值的平均值

Sql server 2008 MS SQL 2008,联接3个表,按T1和T2中的列进行筛选,只查找T3中最小值的平均值,sql-server-2008,Sql Server 2008,我在这里发现了类似但不完全匹配的场景,并且没有任何运气让它起作用。如果这是一个重复的问题,请原谅,我肯定是先搜索的!我有三张桌子。第一个是A,是一张服务台票据清单。第二个,B,时间项列表。第三个是C,第二个是关于时间项的更多细节。对于A中的每个项目,B中可以有多个项目,但对于B中的每个项目,C只有一个条目 wh_任务作为一项任务 task_id create_time source_id ======== ============ =

我在这里发现了类似但不完全匹配的场景,并且没有任何运气让它起作用。如果这是一个重复的问题,请原谅,我肯定是先搜索的!我有三张桌子。第一个是A,是一张服务台票据清单。第二个,B,时间项列表。第三个是C,第二个是关于时间项的更多细节。对于A中的每个项目,B中可以有多个项目,但对于B中的每个项目,C只有一个条目

wh_任务作为一项任务

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\u time\u项目为B

task_id   time_item_id   created_by_user_id
========  ============   =================
1351000   2456           1234567
1351000   2457           2345786
1351000   2458           1234567
wh\u time\u子项为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
高级别目标- 由工程师在给定日期范围内确定每张票据的平均首次响应时间

具体而言— 首先,查找在@StartDate和@EndDate之间创建的所有项目。接下来,在一个源代码为8的文件中查找所有项目(这些是我唯一关心的票证)。然后,我需要找到B中的哪个项目是“第一个”条目,即最接近A中项目创建日期的日期。表B没有创建日期-这是在C中。 一旦我从B中识别出“第一个”的项目,我需要查看由用户创建的项目是否与@Engineer匹配。最后,我想要a.create_time和c.create_time之间日期差的平均值,以分钟为单位表示所有匹配项。响应时间类似于平均值(DATEDIFF(MI,a.create\u time,c.createtime)

在过去的两天里,我已经经历了十几次迭代,这是我现在的坏查询。我知道这个查询,即使它可以运行,也不会给我想要的东西-一直在摆弄它来拉额外的列以进行故障排除:

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爱好者,所以不要担心伤害我的感情。:-

您确定要使用上述查询吗

您知道,朋友……当您直接在
ON子句
语句中使用
aggregate函数
AVG()
时,会发生此错误。像这样:

TableA join TableB on TableA.Code >= AVG(TableB.Code) 
上面的查询有一个类似于您的错误

但是现在您正在使用子查询,在中有一个
聚合函数
,这似乎是正确的。我指的是你询问的这一部分:

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]))

查看此链接:可能是有用的

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_项和wh_time_子项的子查询假定create_time将始终处于wh_任务create_time上或之后。。。这似乎是一个可靠的假设。

这有什么不对(平均值(DATEDIFF(MI,a.create\u time,c.createtime))?执行此操作时是否有任何错误?上面的查询给出了此错误:聚合不能出现在ON子句中,除非它位于HAVING子句或select列表中包含的子查询中,并且被聚合的列是外部引用。不过,我不确定一开始我是否在正确的轨道上,因此此错误可能只是在混淆沃特斯。太棒了,非常感谢,效果很好!很抱歉延迟了响应,我只在项目之间玩SQL,所以直到现在我才能够回到这个话题。