1

I've gone over this code several times but I cannot figure out why I'm getting

Incorrect Syntax near the keyword 'SR'

Here's my SELECT query:

SELECT OrderDetails.OrderID,OrderDetails.ProductCode,OrderDetails.Vendor_Price,OrderDetails.Quantity
FROM OrderDetails
JOIN
(SELECT OrderDetails.OrderID,  
CASE Orders.SalesRep_CustomerID WHEN  1 THEN 'S'  WHEN 2 THEN 'K' WHEN 3 THEN 'M' ELSE '' END 
FROM Orders
GROUP BY OrderDetails.OrderID)
AS 'SR'
WHERE OrderDetails.ShipDate IS NOT NULL
AND OrderDetails.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
ORDER BY OrderDetails.ProductCode ASC

It's pretty straightforward I just don't see the syntax error the computer is referring to. Thanks.

3
  • 5
    Remove the quotes around 'SR' Commented Nov 6, 2013 at 20:24
  • @RUJordan I'm now getting incorrect syntax near 'WHERE' Commented Nov 6, 2013 at 20:26
  • Unquote the table alias - should be SR, not 'SR'. Add the missing 'ON' clause after it; it's JOIN <table> ON <columnA> = <columnB>, and you're missing the ON and columns to JOIN. Commented Nov 6, 2013 at 20:30

2 Answers 2

4

Change 'SR' to SR. There is no need for quotes.

[UPDATE]

You also forget ON statement after AS SR

JOIN (select query...) as SR on SR.OrderID = OrderDetails.OrderID

I guess it should be OrderID if not change it to whatever you need.

[UPDATE 3] REPLACE THE WHOLE QUERY

BTW, I think you wont achieve the result with this query, you need to use the following:

SELECT OrderDetails.OrderID
            ,OrderDetails.ProductCode
            ,OrderDetails.Vendor_Price
            ,OrderDetails.Quantity
            ,CASE Orders.SalesRep_CustomerID WHEN  1 THEN 'S'  WHEN 2 THEN 'K' WHEN 3 THEN 'M' ELSE '' END 
    FROM OrderDetails 
    JOIN #Also consider using INNER JOIN if needed
        Orders on Orders.OrderId = OrderDetails.OrderId
    WHERE 
        OrderDetails.ShipDate IS NOT NULL
        AND OrderDetails.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
    ORDER BY OrderDetails.ProductCode ASC
Sign up to request clarification or add additional context in comments.

4 Comments

you are missing the ON portion of your join, it should be above the where clause.
Sorry about that, yeah you forgot ON statement, updated answer
Thank you but now I'm getting The multi-part identifier "OrderDetails.OrderID" could not be bound.
@henryaaron Use the query I wrote for you in update3! Yours has too many issues.
2

You do not have an ON statement after your JOIN

3 Comments

You got half the problem. :) I can't do half an upvote, though; SO won't let me.
I figured he could combine this answer with the others.
You should answer the entire question, or just comment on the other one providing the rest. As I said, I can't give half an upvote (and the poster can't split an acceptance).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.