Syntax error on CASE Statement join in subquery

Nick 1 Reputation point
2021-07-22T18:28:46.477+00:00

I have the below query and subquery where I am getting a SUM value, and I want to join PS_VOUCHER_LINE LineSub conditionally (on Line.LINE_NBR = LineSub.LINE_NBR) on PS_VOUCHER Line only when a VOUCHER_ID has more than 1 LINE_NBR, otherwise I don't want this last join condition to execute. I am getting a Syntax error on the statement (Incorrect syntax near '='.) . How can I get this conditional join to work properly?

SELECT  
    CONCAT(Header.BUSINESS_UNIT, Header.VOUCHER_ID) AS INVOICE_ID 
    ,   
        ( 

            SELECT SUM(LineSub.MERCHANDISE_AMT) 
            FROM PS_VOUCHER_LINE LineSub
            WHERE Line.BUSINESS_UNIT = LineSub.BUSINESS_UNIT 
            AND Line.VOUCHER_ID = LineSub.VOUCHER_ID 
            AND 
                CASE 
                    WHEN COUNT(Line.LINE_NBR) > 1 THEN Line.LINE_NBR = LineSub.LINE_NBR
                END
            GROUP BY LineSub.VOUCHER_ID 
        ) + Header.FREIGHT_AMT + Header.SALETX_AMT AS GROSS_AMT_LINE_FREIGHT_TAX
FROM 
    PS_VOUCHER Header
    INNER JOIN PS_VOUCHER_LINE Line ON Line.BUSINESS_UNIT = Header.BUSINESS_UNIT 
        AND Line.VOUCHER_ID = Header.VOUCHER_ID
WHERE 
    Header.VOUCHER_ID = '00241107'
Developer technologies Transact-SQL
SQL Server Other
{count} votes

1 answer

Sort by: Most helpful
  1. EchoLiu-MSFT 14,621 Reputation points
    2021-07-23T06:48:10.21+00:00

    Hi @Nick

    Welcome to the microsoft TSQL Q&A forum!

    Sorry,I'm not quite sure about your needs, the following is my guessed solution. If this does not solve your problem, please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …)along with your expected result? So that we’ll get a right direction and make some test.

    Please try:

     SELECT    
         CONCAT(Header.BUSINESS_UNIT, Header.VOUCHER_ID) AS INVOICE_ID   
         ,     
             (   
                  
                 SELECT SUM(LineSub.MERCHANDISE_AMT)   
                 FROM PS_VOUCHER_LINE LineSub  
                 WHERE Line.BUSINESS_UNIT = LineSub.BUSINESS_UNIT   
                 AND Line.VOUCHER_ID = LineSub.VOUCHER_ID   
                 AND Line.LINE_NBR =  
                     (CASE   
                         WHEN COUNT(Line.LINE_NBR) > 1 THEN  LineSub.LINE_NBR  
                     END)  
                 GROUP BY LineSub.VOUCHER_ID   
             ) + Header.FREIGHT_AMT + Header.SALETX_AMT AS GROSS_AMT_LINE_FREIGHT_TAX  
     FROM   
         PS_VOUCHER Header  
         INNER JOIN PS_VOUCHER_LINE Line ON Line.BUSINESS_UNIT = Header.BUSINESS_UNIT   
             AND Line.VOUCHER_ID = Header.VOUCHER_ID  
     WHERE   
         Header.VOUCHER_ID = '00241107'  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.