简体   繁体   English

在同一查询中同时使用CROSS APPLY和INNER JOIN

[英]Using both CROSS APPLY and INNER JOIN in the same query

I'm attempting to use both Left Join and Cross Apply in the same query and running into difficulties. 我试图在同一查询中同时使用“左联接”和“交叉应用”,但遇到了麻烦。

SELECT vAH.TagName, vAH.EventSTamp, -123 Value, vAH.Description, 
--  Ack.DateTime, Ack.UserFullName as AckUser, Ack.Description as AckComment, 
LEFT(vAH.TagName,9) + CONVERT(nvarchar(30),LLC.StartDateTime,113)  as ObjName
FROM WWALMDBArchived.dbo.v_AlarmHistory vAH
--CROSS APPLY (
--  SELECT TOP 1 EventStamp as DateTime, UserFullName, Description
--  FROM WWALMDBArchived.dbo.v_AlarmHistory vAH
--  WHERE TagName = vAH.TagName
--  AND EventStamp > vAH.EventStamp
--  AND AlarmState IN ('ACK_RTN','ACK_ALM')
--  ORDER BY DateTime, UserFullName, Description DESC
--  ) Ack
INNER JOIN CPMS.dbo.LotListConfig LLC
ON vAH.EventStamp >= LLC.StartDateTime
AND vAH.EventStamp <= LLC.EndDateTime
WHERE vAH.TagName LIKE @LineNumber + '%.Action_Alarm_ALM'
AND LLC.LineNumber = @LineNumber
AND LLC.LotNumber = @LotNumber
AND vAH.AlarmState = 'UNACK_ALM'

Essentially what I am doing is getting the boundary information from the LotListConfig table, getting the initial alarm information from v_AlarmHistory, and using the Cross Apply to get some subsequent alarm information from the v_AlarmHistory table. 本质上,我正在做的是从LotListConfig表中获取边界信息,从v_AlarmHistory中获取初始警报信息,并使用Cross Apply从v_AlarmHistory表中获取一些后续警报信息。

The query above returns the records I would expect, but uncommenting the Cross Apply causes no records to return. 上面的查询返回了我期望的记录,但是取消对“交叉应用”的注释不会导致任何记录返回。 There's an interaction of some kind happening between the Inner Join and the Cross Apply that I'm missing. 我缺少的“内部连接”和“交叉应用”之间发生了某种交互。

Anyone? 任何人?

Nevermind. 没关系。

My query above is using the same table shortcut (vAH) in both the main query and the CROSS APPLY query. 我上面的查询在主查询和CROSS APPLY查询中都使用相同的表快捷方式(vAH)。 Deleting the vAH inside the Cross Apply resolves the issue. 在交叉申请中删除vAH可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM