SELECT
A.DJ_NUMBER as djNo
,MAX(A.ACTION) as 'SetupStarted'
,MIN(A.ACTION_TIME) AS 'SetupStart'
,MAX(B.ACTION) as 'RunStarted'
,MAX(B.ACTION_TIME) AS 'UpTimeStart'
,MAX(C.ACTION) as 'RunEnded'
,MAX(C.ACTION_TIME) AS 'UpTimeEnd'
,FORMAT(DATEDIFF(SECOND, MAX(A.ACTION_TIME), MAX(B.ACTION_TIME))/60.0,'##.##') AS Setup
,FORMAT(DATEDIFF(SECOND, MAX(B.ACTION_TIME), (C.ACTION_TIME))/60.0, '##.##') AS UpTime
FROM
[AFL_MES2O_Interface_Data_arch] A,
[StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch] B,
[StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch] C
--WHERE (B.DJ_NUMBER = A.DJ_NUMBER AND MAX(B.ACTION_TIME) > Min(A.ACTION_TIME)) and (C.DJ_NUMBER = B.DJ_NUMBER AND MAX(C.ACTION_TIME) > MAX(B.ACTION_TIME))
GROUP BY
A.DJ_NUMBER
HAVING
(B.DJ_NUMBER = A.DJ_NUMBER
AND MAX(B.ACTION_TIME) > Min(A.ACTION_TIME))
AND (C.DJ_NUMBER = B.DJ_NUMBER
AND MAX(C.ACTION_TIME) > MAX(B.ACTION_TIME))
ORDER BY
SetupStart ASC, UpTimeEnd
这是我遇到的错误:
信息8121,第16级,状态1,第15行
在HAVING子句中,“ StagingDB.dbo.AFL_MES2O_Interface_Data_arch.DJ_NUMBER”列无效,因为它不在聚合函数或GROUP BY子句中。信息8121,第16级,状态1,第15行
在HAVING子句中,“ StagingDB.dbo.AFL_MES2O_Interface_Data_arch.DJ_NUMBER”列无效,因为它不在聚合函数或GROUP BY子句中。信息8121,第16级,状态1,第15行
在HAVING子句中,“ StagingDB.dbo.AFL_MES2O_Interface_Data_arch.DJ_NUMBER”列无效,因为它不在聚合函数或GROUP BY子句中。
答案 0 :(得分:1)
旧式联接的最大问题之一是您将联接条件与查询的过滤逻辑混合在一起,因为您将它们都放在了where
子句中。在这里,您试图将所有这些都放在where
子句中,但遇到一个错误,即聚合可能不会出现在where
中,但应该出现在having
中,并且将所有条件都移到了having
只是为了获得上述错误。因此,要解决此问题,请将连接条件保留在where
子句中:
WHERE (B.DJ_NUMBER = A.DJ_NUMBER) and (C.DJ_NUMBER = B.DJ_NUMBER)
和having
中的聚合:
HAVING (MAX(B.ACTION_TIME) > Min(A.ACTION_TIME)) and (MAX(C.ACTION_TIME) > MAX(B.ACTION_TIME))
正如其他人在评论中所建议的那样,您实际上应该停止使用旧样式的联接。用[inner join][1]
重写查询:
FROM
[AFL_MES2O_Interface_Data_arch] A
inner join [StagingDB].[dbo].[@AFL_MES2O_Interface_Data_arch] B on B.DJ_NUMBER = A.DJ_NUMBER
inner join [StagingDB].[dbo].[@AFL_MES2O_Interface_Data_arch] C on C.DJ_NUMBER = B.DJ_NUMBER
在这种情况下,您将不再需要where
子句,您将在having
中写入聚合,如上所示。
此外,您的UpTime
子句的select
表达式中有一个错误-无法C.ACTION_TIME
这样保留。它应该在聚合函数中,例如MAX(C.ACTION_TIME)
。
答案 1 :(得分:0)
我明白了。解决方法如下:
SELECT
A.DJ_NUMBER AS JobNo
,MAX(A.ACTION) as SetupStarted
,MIN(A.ACTION_TIME) AS SetupStart
-- ,MAX(A.ACTION_TIME) AS 'SetupStartEnd'
,MAX(B.ACTION) as RunStarted
-- ,MIN(B.ACTION_TIME) as 'RunStarted'
,MAX(B.ACTION_TIME) AS UpTimeStart
,MAX(C.ACTION) as 'RunEnded'
-- ,MIN(C.ACTION_TIME) as 'UpTimeEndStart'
,MAX(C.ACTION_TIME) AS UpTimeEnd
,FORMAT(DATEDIFF(SECOND, MIN(A.ACTION_TIME), MAX(B.ACTION_TIME))/60.0,'##.##') AS Setup
,FORMAT(DATEDIFF(SECOND, MAX(B.ACTION_TIME), MAX(C.ACTION_TIME))/60.0,'##.##') AS UpTime
FROM
[StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch] as A
INNER JOIN [StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch] as B
ON (B.DJ_NUMBER = A.DJ_NUMBER AND A.ACTION_TIME IS NOT NULL) AND B.ACTION_TIME > A.ACTION_TIME
INNER JOIN [StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch] as C
ON (C.DJ_NUMBER = B.DJ_NUMBER AND B.ACTION_TIME IS NOT NULL) AND C.ACTION_TIME > B.ACTION_TIME
GROUP BY A.DJ_NUMBER
--ORDER BY SetupStart ASC, UpTimeEnd ASC
--where TubeJobNo = 20738056