Solved

SQL Syntax with JOIN

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
multiple application databases same MSSQL instance 5 52
Access sql to sql server express 10 31
sql server concatenate fields 10 31
T-SQL: "HAVING CASE" Clause 1 23
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

813 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

8 Experts available now in Live!

Get 1:1 Help Now