sql - 添加子查询时查询语法问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql - 添加子查询时查询语法问题相关的知识,希望对你有一定的参考价值。

我似乎无法执行此查询没有任何语法错误,我收到以下错误:

无法绑定多部分标识符“TBL1.Avg_CycleTime”。

查询:

SELECT (AVG(TBL1.Avg_CycleTime + TBL4.Avg_GRM_CycleTime)) AS Overall_Renewals_CycleTime
FROM (
    SELECT *
    FROM (
        SELECT AVG(1.00 * Avg_DayDiff) AS Avg_CycleTime
        FROM (
            SELECT AVG(1.00 * DATEDIFF(DAY, yy, xx)) AS Avg_DayDiff
            FROM Database1.dbo.tbl_1
            WHERE month(datecompleted) = month(dateadd(month, - 1, current_timestamp))
                AND year(datecompleted) = year(dateadd(month, - 1, current_timestamp))
            ) t1
        WHERE Avg_DayDiff > 0
        ) TBL1
    CROSS JOIN (
        SELECT *
        FROM (
            SELECT AVG(1.00 * Avg_DayDiff) AS Avg_GRM_CycleTime
            FROM (
                SELECT DATEDIFF(DAY, xx, uu) AS Avg_DayDiff
                FROM Database1.dbo.tbl_1
                WHERE month(datecompleted) = month(dateadd(month, - 1, current_timestamp))
                    AND year(datecompleted) = year(dateadd(month, - 1, current_timestamp))
                    AND ApprovalRequiredFrom = 'GRM'
                ) t2
            WHERE Avg_DayDiff > 0
            ) TBL2
        ) TBL3
    ) TBL4

有人可以告诉我我做错了什么吗?我想在(Avg_cycletime和avg_grm_cycletime)之间获得AVERAGE

干杯,

答案

- 在您的选择中,您正在从TBL1中选择值(在子查询中,您的FROM中不存在。将其更改为TBL4

select (AVG(TBL1.Avg_CycleTime + TBL4.Avg_GRM_CycleTime)) as Overall_Renewals_CycleTime
from ()
  )TBL4

此外,我不会使用这么多嵌套选择,或者至少不使用Select *(选择*永远不是好主意)。

以上是关于sql - 添加子查询时查询语法问题的主要内容,如果未能解决你的问题,请参考以下文章

带有子查询的 redshift sql 查询中的语法错误

CTE内部有多个子查询

sql语句增删改查与子查询

SQL server 建立的触发器子查询返回值不唯一,需要用多个返回值如何操作

是否可以从子查询中添加 SQL 别名?

MS Access/SQL 子查询的语法,包括聚合函数