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.