troubleshooting Question

SQL - Syntax Query Damn Bracket :)

Avatar of Zack
ZackFlag for Australia asked on
SQLMicrosoft SQL Server
8 Comments3 Solutions16 ViewsLast Modified:
Hi EE,

Having an issue with the last bracket in this query;
SELECT  DISTINCT A.* FROM (SELECT
                     a.ACCOUNTNUM, CAST(a.TRANSDATE AS date) AS transdate, a.VOUCHER, a.INVOICE, a.CURRENCYCODE, a.TXT, c.NAME, CAST(a.DUEDATE AS date)
                      AS duedate, d.CUSTGROUP, a.AMOUNTCUR, a.SETTLEAMOUNTCUR, a.SETTLEAMOUNTMST, a.SETTLEAMOUNTMST AS ORIGSETTELEMST, a.AMOUNTMST,
                      d.CREDITMAX, a.LASTSETTLEVOUCHER, f.INVOICEID AS lastsettleinvoice, a.TRANSTYPE, a.EXCHADJUSTMENT AS SELISIHKURS,
                      CASE WHEN A.AMOUNTMST < 0 THEN A.AMOUNTMST ELSE 0 END AS MSTCREDIT,
                      CASE WHEN A.AMOUNTCUR < 0 THEN A.AMOUNTCUR ELSE 0 END AS CURCREDIT, CASE WHEN A.AMOUNTMST > 0 THEN A.AMOUNTMST ELSE 0 END AS MSTDEBET,
                       CASE WHEN A.AMOUNTCUR > 0 THEN A.AMOUNTCUR ELSE 0 END AS CURDEBET,
                      CASE WHEN a.AMOUNTMST - a.SETTLEAMOUNTMST <> 0 THEN a.AMOUNTMST - a.SETTLEAMOUNTMST + a.EXCHADJUSTMENT ELSE a.AMOUNTMST - a.SETTLEAMOUNTMST
                       END AS SISA, CASE WHEN A.CURRENCYCODE <> 'AUD' THEN a.AMOUNTCUR - a.SETTLEAMOUNTCUR ELSE 0 END AS SISACUR, CASE WHEN LEFT(A.VOUCHER, 4)
                      <> 'CINV' THEN A.VOUCHER ELSE '' END AS REFERENSI
                 , CASE WHEN  LEFT(A.INVOICE  ,3) IN( 'FTX', 'CFT','NK/','ND/') and LEFT(A.INVOICE  ,3) not  IN( 'SCN') THEN 'FREETEXT'
                 ELSE CASE WHEN  SUBSTRING(A.INVOICE,2,2)='KR' THEN 'PAYMENT'
                 ELSE CASE WHEN  LEFT(A.INVOICE ,3) IN( 'SCN') THEN 'RETURN'
                 ELSE CASE WHEN  LEFT(A.VOUCHER ,4)  IN ('15TM','16TM','ARME') THEN 'PAYMENT'
                 ELSE CASE WHEN  A.AMOUNTMST > 0 AND LEFT(A.INVOICE  ,3)NOT IN( 'FTX', 'CFT', 'SCN','NK/','ND/') AND LEFT(A.VOUCHER ,4) NOT IN ('15TM','16TM','ARME') AND SUBSTRING(A.INVOICE,2,2)<>'KR' THEN 'PENJUALAN'
                 --ELSE CASE WHEN  A.AMOUNTMST > 0 AND LEFT(A.INVOICE  ,3)NOT IN( 'FTX', 'CFT', 'SCN','NK/','ND/') AND LEFT(A.VOUCHER ,4)  IN ('15TM','16TM','ARME') THEN 'PAYMENT'
                 ELSE CASE WHEN  A.AMOUNTMST < 0 AND LEFT(A.INVOICE  ,3) NOT IN( 'FTX', 'CFT','SCN','NK/','ND/') THEN 'PAYMENT'
                 END END END END END END
               REPORT_POST ,a.RECID
FROM         CUSTTRANS AS a LEFT OUTER JOIN
                      CUSTTABLE AS b ON a.ACCOUNTNUM = b.ACCOUNTNUM LEFT OUTER JOIN
                      DIRPARTYTABLE AS c ON b.PARTY = c.RECID LEFT OUTER JOIN
                      CUSTTABLE AS d ON a.ACCOUNTNUM = d.ACCOUNTNUM LEFT OUTER JOIN
                      CUSTINVOICEJOUR AS f ON a.LASTSETTLEVOUCHER = f.LEDGERVOUCHER LEFT OUTER JOIN)) A

Msg 102, Level 15, State 1, Line 24
Incorrect syntax near ')'.

Any assistance is appreciated.

Thank you. 

SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros