Solved

SQL Syntax with JOIN

Posted on 2013-01-28
3
315 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

910 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

20 Experts available now in Live!

Get 1:1 Help Now