Solved

SQL Syntax with JOIN

Posted on 2013-01-28
3
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

623 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