Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Syntax with JOIN

Posted on 2013-01-28
3
Medium Priority
?
325 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

886 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