子查询的左连接在SQL Server中不起作用

时间:2014-04-22 15:20:55

标签: sql-server-2008 subquery left-join

我在添加子查询作为左连接时遇到了很多麻烦,当我将组合查询作为单独的部分运行时,每个部分都可以工作,但是当我尝试运行整个事情时,它失败了,我&#39 ; ve尝试修复语法并更改字母并添加括号但似乎没有任何帮助,查询如下:

 SELECT *
 FROM
  (SELECT Sales_Doc_Type,
          Doc_Date,
          Sales_Doc_Num,
          Sales_Person_ID,
          Customer_Name,
          Shipping_Method,
          Total,
          Subtotal,
          xIntFreight,
          xSalesmanCost,
          SOURCE,
          xCommisionPaid,
          Payment_Terms
   FROM [VISPR].[dbo].[spv3SalesDocument]
   WHERE Sales_Doc_Type = 'invoice'
     AND Sales_Person_ID = 'xx01'
   UNION ALL SELECT Sales_Doc_Type,
                    Doc_Date,
                    Sales_Doc_Num,
                    Sales_Person_ID,
                    Customer_Name,
                    Shipping_Method,
                    Total,
                    Subtotal,
                    xIntFreight,
                    xSalesmanCost,
                    SOURCE,
                    xCommisionPaid,
                    Payment_Terms
   FROM [VISPR].[dbo].[spv3SalesDocumentHistory]
   WHERE Sales_Doc_Type = 'invoice'
     AND Sales_Person_ID = 'xx01'
     AND SOURCE NOT IN ('Void')) AS a
INNER JOIN [VISPR].[dbo].[SOP30200] AS b ON a.Sales_Doc_Num = b.SOPNUMBE
AND a.Sales_Doc_Type = b.DOCID
LEFT JOIN [VISPR].[dbo].[spvSalesDocumentTrackingNumber] AS c ON a.Sales_Doc_Num = c.Sales_Doc_Num
AND a.Sales_Doc_Type = c.Sales_Doc_Type AS d
LEFT JOIN
  (SELECT DISTINCT x.Sales_Doc_Num,
                   x.PO_Number,
                   y.xPOComm
   FROM [VISPR].[dbo].[spvSalesLinePO] AS x,
        VISPR.dbo.spxPurchaseOrder AS y
   WHERE x.PO_Number = y.PO_Number) AS e ON d.Sales_Doc_Num = e.Sales_Doc_Num

当我在"之前选择所有内容时,作为d"当我选择形成" e"的子查询时,执行我得到的结果。它运行但当我尝试执行整个事情时,我收到此错误:

  

第15行,第15行,第1行,第13行   关键字'附近的语法不正确为'。
  消息156,15级,状态1,行16   关键字'附近的语法错误为'。

感谢任何帮助,我之前已经能够解决这些类型的问题,但它通常与语法或结构相关,在这里我只能在经过数小时的尝试和搜索答案后得到它。表格/查询别名是否有限制,即使我似乎没有这么多?

1 个答案:

答案 0 :(得分:1)

使用此更新您的查询。

SELECT *
FROM
(SELECT Sales_Doc_Type,
      Doc_Date,
      Sales_Doc_Num,
      Sales_Person_ID,
      Customer_Name,
      Shipping_Method,
      Total,
      Subtotal,
      xIntFreight,
      xSalesmanCost,
      SOURCE,
      xCommisionPaid,
      Payment_Terms
FROM [VISPR].[dbo].[spv3SalesDocument]
WHERE Sales_Doc_Type = 'invoice'
 AND Sales_Person_ID = 'xx01'
UNION ALL SELECT Sales_Doc_Type,
                Doc_Date,
                Sales_Doc_Num,
                Sales_Person_ID,
                Customer_Name,
                Shipping_Method,
                Total,
                Subtotal,
                xIntFreight,
                xSalesmanCost,
                SOURCE,
                xCommisionPaid,
                Payment_Terms
FROM [VISPR].[dbo].[spv3SalesDocumentHistory]
WHERE Sales_Doc_Type = 'invoice'
 AND Sales_Person_ID = 'xx01'
 AND SOURCE NOT IN ('Void')) AS a
INNER JOIN [VISPR].[dbo].[SOP30200] AS b ON a.Sales_Doc_Num = b.SOPNUMBE
AND a.Sales_Doc_Type = b.DOCID
LEFT JOIN [VISPR].[dbo].[spvSalesDocumentTrackingNumber] AS c ON a.Sales_Doc_Num = c.Sales_Doc_Num
 AND a.Sales_Doc_Type = c.Sales_Doc_Type
LEFT JOIN
(SELECT DISTINCT x.Sales_Doc_Num,
               x.PO_Number,
               y.xPOComm
 FROM [VISPR].[dbo].[spvSalesLinePO] AS x,
    VISPR.dbo.spxPurchaseOrder AS y
WHERE x.PO_Number = y.PO_Number) AS e ON c.Sales_Doc_Num = e.Sales_Doc_Num