简体   繁体   English

“无法绑定多部分标识符”错误

[英]“multi-part identifier could not be bound” error

I'm using SQL Server 2005. I get this error: 我正在使用SQL Server2005。出现此错误:

*The multi-part identifier "ms.MOP_desc" could not be bound.* *无法绑定多部分标识符“ ms.MOP_desc”。*

I tested each of the two select queries and they worked fine individually, but I got the error when I union those queries. 我测试了两个选择查询中的每一个,它们分别工作正常,但是在合并这些查询时出现了错误。 Can I anyone tell me what went wrong with this query? 我可以告诉我此查询出了什么问题吗? Thank you. 谢谢。

SELECT SUM(Amount) AS TotalAmount, ms.MOP_desc
FROM 
(
    SELECT SUM(hd.delivery_value) AS Amount, ms.MOP_desc
    FROM TRANSACTION_HEADER AS th 
        INNER JOIN TRANSACTION_DETAIL AS td 
            ON th.transaction_number = td.transaction_number 
        LEFT JOIN hose_delivery hd 
            ON td.delivery_id = hd.delivery_id 
        LEFT JOIN product pr 
            ON pr.product_id = td.product_id
        INNER JOIN MOP_Setting AS ms 
            ON hd.MOP_ID = ms.MOP_ID  
    WHERE hd.delivery_value > 0 
        AND (th.USER_PERIOD_ID IN (13))
        AND (hd.MOP_ID  IN (1))
        AND hd.Cleared_By != '0'
    GROUP BY ms.MOP_desc

    UNION ALL

    SELECT  SUM(td.quantity * td.price_sold) AS Amount, ms.MOP_desc 
    FROM TRANSACTION_HEADER AS th 
        INNER JOIN TRANSACTION_DETAIL AS td 
            ON th.transaction_number = td.transaction_number 
        INNER JOIN MOP_Setting AS ms 
            ON th.MOP_ID = ms.MOP_ID  
    WHERE (th.USER_PERIOD_ID IN (13))
        AND (th.MOP_ID  IN (1))
    GROUP BY ms.MOP_desc
)t

Because the result from the UNION ed query is a rowset that you've given the alias t to - the ms alias is no longer applicable: 因为UNION ed查询的结果是一个行集,您已为其赋予别名t ,所以ms别名不再适用:

SELECT SUM(Amount) AS TotalAmount, t.MOP_desc
FROM 
(
...
)t

If this really is a second SUM step here, you'll need a outer GROUP BY clause also. 如果这确实是第二个SUM步骤,则还需要一个外部GROUP BY子句。

When you select from a subselect instead of a table, the column names from the subselect are not exposed to the outer select 当您从子选择而不是表中进行选择时,子选择中的列名不会公开给外部选择

Use 采用

SELECT SUM(Amount) AS TotalAmount, mop
FROM 
(
  SELECT SUM(hd.delivery_value) AS Amount, ms.MOP_desc as mop
  FROM TRANSACTION_HEADER AS th 
      INNER JOIN TRANSACTION_DETAIL AS td 
...

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

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