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

时间:2018-03-29 16:34:53

标签: sql sql-server subquery

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

  

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

QUERY:

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

有人可以告诉我我做错了什么吗?我希望得到AVERAGE(Avg_cycletime& avg_grm_cycletime)

欢呼声,

1 个答案:

答案 0 :(得分:2)

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

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

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