Solved

SQL Syntax with JOIN

Posted on 2013-01-28
3
314 Views
Last Modified: 2013-01-29
I have this query that I needed to update with a select statement joining 3 other tables. I have marked below where the new code begins and ends. When I parse the code I get "Invalid syntax near ON" I know this means a syntax error. What would be the proper syntax be to add what is in the join to the criteria of this query?

SELECT (case PATINDEX ('%CMP%',jl.jobnumber) when 0 then 'DYN' else 'CMP' end) as COMPANYID,
       rtrim(jl.jobnumber) as jobnumber,jl.transnmbr,jl.mastertype,jl.masterid,pm.vendname as mastername,jl.debitamt,jl.crdtamnt,      
         left(jl.DISTREF,31) as TRXDSCRN,jl.docsource,case when coalesce(ph.voided,0) = 0 then 0 else 1 end as VOIDSTTS,jl.actnumst,
       case when gl.ACCATNUM = 31 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as laboramt,
         case when gl.ACCATNUM = 32 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as Matlamnt,
       case when gl.ACCATNUM = 33 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as delivamt,
         case when gl.ACCATNUM = 34 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as Outsvcamt,
       case when gl.ACCATNUM = 35 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as Miscamnt,
       case when gl.ACCATNUM = 36 then case when coalesce(ph.voided,0) = 0 then jl.debitamt - jl.crdtamnt else jl.crdtamnt - jl.debitamt end else 0 end as ciasamnt
      FROM [GSE]..[gl00100] gl, [GSE]..[pm00200] pm, [GSE]..[job_linker] as jl
    inner join (
      SELECT distinct rtrim(PACONTNUMBER) as pacontnumber
      FROM [GSE]..[PA01101]
               ) as P
            on jl.jobnumber = p.PACONTNUMBER

--- THIS IS WHERE THE NEW CODE FOR THE JOIN WAS ADDED

      JOIN (select t1.VCHRNMBR,t1.DOCTYPE,t1.date1 AS DOCDATE,t1.glpostdt AS POSTDATE,T2.DINVPDOF,T2.VOIDPDATE,T3.DOCNUMBR
            from (Select * from PM30300) t1
            JOIN (SELECT vendorid,vchrnmbr,doctype,docdate,pstgdate,DOCNUMBR,DINVPDOF,VOIDPDATE
                  FROM PM30200 WHERE DOCTYPE IN (1,2,3)) t2
                  ON T1.VENDORID=T2.VENDORID AND t1.APTVCHNM=t2.VCHRNMBR AND t1.APTODCTY=t2.DOCTYPE
            JOIN (SELECT vendorid,vchrnmbr,doctype,docdate,pstgdate,DOCNUMBR
                  FROM PM30200 WHERE DOCTYPE = 6) t3
                  ON t1.VENDORID=T3.VENDORID AND t1.VCHRNMBR=t3.VCHRNMBR AND t1.DOCTYPE=t3.DOCTYPE
            WHERE t1.DOCTYPE=6)
            ON jl.transnmbr = t1.vchrnmbr and jl.docnumbr = t1.docnumbr

-- THIS IS WHERE THE NEW CODE ENDED

where t1.glpostdt >= '03/01/2011' and jl.mastertype = 'PAD' and len(rtrim(jl.jobnumber))>0
      and jl.masterid = pm.vendorid and gl.actnumbr_1=jl.actnumst
      and gl.accatnum>=31 and jl.docsource<>'Access_Import'
0
Comment
Question by:rwheeler23
3 Comments
 
LVL 4

Assisted Solution

by:MimicTech
MimicTech earned 250 total points
ID: 38829721
 JOIN (
   select
    t1.VCHRNMBR
   ,t1.DOCTYPE
   ,t1.date1 AS DOCDATE
   ,t1.glpostdt AS POSTDATE
   ,T2.DINVPDOF
   ,T2.VOIDPDATE
   ,T3.DOCNUMBR
   from (Select * from PM30300) t1
   JOIN (
      SELECT 
       vendorid   <- can be removed
      ,vchrnmbr  <- can be removed
      ,doctype  <- can be removed
      ,docdate  <- can be removed
      ,pstgdate  <- can be removed
      ,DOCNUMBR  <- can be removed
      ,DINVPDOF
      ,VOIDPDATE
      FROM PM30200 
      WHERE DOCTYPE IN (1,2,3)) t2
   ON T1.VENDORID=T2.VENDORID 
   AND t1.APTVCHNM=t2.VCHRNMBR 
   AND t1.APTODCTY=t2.DOCTYPE
   JOIN (
       SELECT 
       vendorid <- can be removed
      ,vchrnmbr <- can be removed
      ,doctype <- can be removed
      ,docdate <- can be removed
      ,pstgdate <- can be removed
      ,DOCNUMBR
      FROM PM30200 WHERE DOCTYPE = 6) t3
   ON t1.VENDORID=T3.VENDORID 
   AND t1.VCHRNMBR=t3.VCHRNMBR 
   AND t1.DOCTYPE=t3.DOCTYPE
WHERE t1.DOCTYPE=6) as abc
ON jl.transnmbr = abc.vchrnmbr 
and jl.docnumbr = abc.docnumbr

Open in new window



Towards the bottom look for "abc" that should fix it

Note: joined queries do not need all the columns, only the columns that the main select needs. So in the main select, the columns labeled t2.* are the only ones that need to be in the joined t2 query, etc. This makes things cleaner.
0
 
LVL 10

Accepted Solution

by:
deviprasadg earned 250 total points
ID: 38830411
1. First name is missing for the Join table query. Give it a name and refer that name in the Subsequent on clause.

2. The new code added does not have t1.docnumbr in the select list. Please add this and use it in the on clause.
0
 

Author Closing Comment

by:rwheeler23
ID: 38833069
Old habits are hard to break. I just have always asked only for the columns I need. Thank you both. The query now runs.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to fix this error 14 48
SQL index creation taking an abnormally long time 2 20
Change part of a string 2 24
Join vs where 2 11
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now