子查询中具有多部分标识符问题的 SQL 查询
我有一个查询应该返回“状态”持续时间条目的总和。持续时间是通过使用 datediff(n, datestamp, (返回结束当前状态的日期戳的子查询,即在锁定的条目之后找到下一个合适的“状态更改”条目)来计算的。
我的问题是以下查询返回一个多-部分标识符错误
- INC 表给了我 我正在寻找与以下内容相关的“INCIDENT_NUMBER” 其他表中的“NUMBER”
- ACTM1 保存所有 DATESTAMP 条目
- ACTA1 通过“THENUMBER”与 ACTM1 相关,并且它保存有关条目是否适合状态更改的所有信息
代码:
SELECT SUM(DATEDIFF(n, ACTM1.DATESTAMP, END_DATESTAMP_TABLE.END_DATESTAMP))
FROM INC LEFT OUTER JOIN
ACTM1 ON INC.INCIDENT_NUMBER = ACTM1.NUMBER LEFT OUTER JOIN
ACTA1 ON ACTM1.THENUMBER = ACTA1.THENUMBER LEFT OUTER JOIN
/**/
(SELECT ACTM1_1.NUMBER, ACTM1_1.DATESTAMP AS END_DATESTAMP
FROM ACTM1 AS ACTM1_1 LEFT OUTER JOIN
/**/
(SELECT ACTM1_1_1.NUMBER, MIN(ACTM1_1_1.THENUMBER) AS FOLLOWUP_THENUMBER
FROM ACTM1 AS ACTM1_1_1
WHERE (ACTM1_1_1.THENUMBER > /**/ ACTM1_1.THENUMBER)/*I think here lies the problem*/
AND (ACTM1_1_1.[TYPE] IN ('Open', 'Status Change', 'Resolved', 'Closed')))
AS FOLLOWUP_THENUMBER_TABLE
/**/
ON ACTM1_1.NUMBER = FOLLOWUP_THENUMBER_TABLE.NUMBER)
AS END_DATESTAMP_TABLE
/**/
ON ACTM1.NUMBER = END_DATESTAMP_TABLE.NUMBER
WHERE ...
我将不胜感激任何有用的评论或提示你可以给我这个,
PS
i've a query that is supposed to return the sum for "status"-duration entries. The duration is calculated by using datediff(n, datestamp, (subquery that returns the datestamp ending the current status, i.e. finds the next fitting "status change"-entry after the one locked at)
My Problem is that the following query returns an multi-part identifier error
- The INC table is giving me the
"INCIDENT_NUMBER" i'm looking for wich is related to
"NUMBER" in the other tables - ACTM1 holds all DATESTAMP-Entries
- ACTA1 is related to ACTM1 via "THENUMBER" and it holds all the information about if an entry is an fitting status change or not
Code:
SELECT SUM(DATEDIFF(n, ACTM1.DATESTAMP, END_DATESTAMP_TABLE.END_DATESTAMP))
FROM INC LEFT OUTER JOIN
ACTM1 ON INC.INCIDENT_NUMBER = ACTM1.NUMBER LEFT OUTER JOIN
ACTA1 ON ACTM1.THENUMBER = ACTA1.THENUMBER LEFT OUTER JOIN
/**/
(SELECT ACTM1_1.NUMBER, ACTM1_1.DATESTAMP AS END_DATESTAMP
FROM ACTM1 AS ACTM1_1 LEFT OUTER JOIN
/**/
(SELECT ACTM1_1_1.NUMBER, MIN(ACTM1_1_1.THENUMBER) AS FOLLOWUP_THENUMBER
FROM ACTM1 AS ACTM1_1_1
WHERE (ACTM1_1_1.THENUMBER > /**/ ACTM1_1.THENUMBER)/*I think here lies the problem*/
AND (ACTM1_1_1.[TYPE] IN ('Open', 'Status Change', 'Resolved', 'Closed')))
AS FOLLOWUP_THENUMBER_TABLE
/**/
ON ACTM1_1.NUMBER = FOLLOWUP_THENUMBER_TABLE.NUMBER)
AS END_DATESTAMP_TABLE
/**/
ON ACTM1.NUMBER = END_DATESTAMP_TABLE.NUMBER
WHERE ...
I would be grateful for any helpful comment or hint you could give me on this,
PS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
左侧联接关系无法引用右侧,因此这是非法的:
请改用 APPLY 运算符:
在您的情况下可能如下所示:
显然,内部查询中的 MIN 没有任何意义。
The left side join relation cannot reference the right side, so this is illegal:
Use the APPLY operator instead:
In your case would probably be like following:
Obviously the MIN inside the inner query makes no sense though.
我会重写查询以根本不使用子查询:
我无法完全对您的语句进行逆向工程。我不知道你是否需要左连接,而且我没有看到 ACTA1 实际在哪里使用,所以我把它省略了。因此,您可能需要调整上述内容。但总体思路是找到具有更大数字的行,该行具有您需要的类型,但没有其他行 (A3) 具有正确的类型,并且数字位于两个数字之间。
I would rewrite the query to not use subqueries at all:
I wasn't able to fully reverse engineer your statement. I don't know if you needed the left joins or not and I didn't see where ACTA1 was actually being used, so I left it out. As a result, you may need to tweak the above. The general idea though is to find a row with a greater number, which has the type that you need, but for which there is no other row (A3) with the right type and a number that falls in between the two numbers.