title: Incorrect syntax near ')' on the last parenthesis when changing from openquery to a subquery on a local database
body: <p>Why does this query return "Incorrect syntax near ')' " on the last parenthesis when changing from openquery to a subquery on a local database?</p>
  
  <p>You can see the commented lines where the openquery started before (and worked)</p>
  
  <pre><code>select right(rtrim(a.vouchernumber),9) as voucherkey
  , a.vouchernumber
  , vendorkey
  , rtrim(invoicenumber) as invoicenumber
  , invoicedate
  , duedate
  , documenttype
  , PostDate
  , qty
  , amt = 
      CASE 
          WHEN documenttype = 'D' THEN -(amt)
          WHEN b.transactiontype = 'V' THEN -(amt)
      ELSE 
          amt 
      End
  , extendedlineamt
  , intercompanyid
  , acct
  , NaturalAcct
  , c.segmentdescription as NaturalAcctDesc
  , Dept
  , f.segmentdescription as DeptDesc
  , Site
  , d.segmentdescription as SiteDesc
  , Project
  , e.segmentdescription as ProjDesc
  , b.*
  , rtrim(vendorname) as vendorname
  , rtrim(vendoraddress1) as vendoraddress1
  , vendoraddress2
  , vendoraddress3
  , vendorcity
  , rtrim(vendorstate) as vendorstate
  , rtrim(vendorzipcode) as vendorzipcode
  , rtrim(vendoraddress1) + ' ' + rtrim(vendoraddress2) +' ' + rtrim(vendorcity) + ', ' + rtrim(vendorstate) as VendorAddress
  , Dept + '.' + Site + '.' + Project as ProjectId 
  --from openquery (LinkedServer,
  --  'select a.vouchernumber
  from  (
      select a.vouchernumber
      , a.vendorkey
      , a.invoicenumber
      , a.invoicedate
      , a.duedate
      , a.documenttype
      , a.recdate as PostDate
      , b.qty
      , b.amt
      , b.extendedlineamt
      , b.intercompanyid
      , b.acct
      , Substring(Acct, 1, 4) as NaturalAcct
      , Substring(Acct,8, 3 ) as Site
      , Substring(Acct,11, 4 ) as Project
      , Substring(Acct,5,3) as Dept
      , v.vendorname
      , v.vendoraddress1
      , v.vendoraddress2
      , v.vendoraddress3
      , v.vendorcity
      , v.vendorstate
      , v.vendorzipcode 
  from aphdr a 
  join aplin b 
      on a.vouchernumber = b.vouchernumber
  left join apvend v 
      on a.vendorkey = v.vendorkey
  where a.recdate between '2011-01-01' and '2012-10-02' 
      and cast(Substring(Acct,11, 4 ) as varchar(4)) like 'VIR'
      and left(a.vouchernumber,1) &lt;&gt; 'G'
      and Company = 'ASSFD'
  )
  </code></pre>