Question

I'm having a lot of trouble adding a subquery in as a left join, when I run the combined query as separate parts each piece works, but when I try to run the whole thing it fails and I've tried to fix the syntax and change lettering and adding parentheses but nothing seems to help, query is below:

 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

When I select everything before "as d" and execute I get results, when I select just the subquery forming "e" it runs but when I try to execute the entire thing I get this error:

Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'as'.

Any help is appreciated, I've been able to figure these types of issues out before but it was usually syntax or structure related, here I just can't get it after hours of trying and searching for answers. Could there be a limit on table/query aliases, even though I don't seem to have that many?

Was it helpful?

Solution

update your query with this one.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top