Solved

Conditional Inner Join

Posted on 2004-04-05
8
1,520 Views
Last Modified: 2012-06-21
I have two tables that I can link together with a common field.  However there is an instance where there could be more than one value in the second field.  For example..

Field1        Field2        Field3
1234         555            
3321         865
2956         362           GODNAT
2956         363           HEJ


I would like to be able to link on Field1 unless Field3 is pupulated, and then use the value in Field2 in my results.

Any suggestions would be a huge help!

/Atropa
0
Comment
Question by:Atropa
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 11

Expert Comment

by:vc01778
ID: 10760380
You can do this:

select ...  from
table1 join
 (select  case when field3 is null then field1 else field2 end field_x, .., from table2)  t2 on table1.id=t2.field_x


...here field_x is a field computed from field1/field2/field3.


VC
0
 
LVL 5

Author Comment

by:Atropa
ID: 10760522
okay, so here is what I have yet I am getting a message saying..
Incorrect syntax near the keywork 'ON'.

FROM bbadger.tblcommhmo2
      INNER JOIN dbo.tbl_IDXeGrps
      ON bbadger.tblcommhmo2.XMAP_IDX_HP_NM = dbo.tbl_IDXeGrps.HealthPlan
      INNER JOIN
            (SELECT CASE
            WHEN schandler.tblNWOC_IDX_PC_XMAP.PLANCODE_ORG IS NULL
            THEN schandler.tblNWOC_IDX_PC_XMAP.NWOC_PC
            ELSE schandler.tblNWOC_IDX_PC_XMAP.PLANCODE_ORG
            END Link_Field
            FROM schandler.tblNWOC_IDX_PC_XMAP)
            ON bbadger.tblcommhmo2.IDX_PLANNUMBER = schandler.tblNWOC_IDX_PC_XMAP.Link_Field

schandler.tblNWOC_IDX_PC_XMAP is the field mentioned above.  I have never used a case statement in SQL only VB so I am not fully aware of the syntax.
0
 
LVL 11

Accepted Solution

by:
vc01778 earned 500 total points
ID: 10760556
It should be:

FROM bbadger.tblcommhmo2
     INNER JOIN dbo.tbl_IDXeGrps
     ON bbadger.tblcommhmo2.XMAP_IDX_HP_NM = dbo.tbl_IDXeGrps.HealthPlan
     INNER JOIN
          (SELECT CASE
          WHEN schandler.tblNWOC_IDX_PC_XMAP.PLANCODE_ORG IS NULL
          THEN schandler.tblNWOC_IDX_PC_XMAP.NWOC_PC
          ELSE schandler.tblNWOC_IDX_PC_XMAP.PLANCODE_ORG
          END Link_Field
          FROM schandler.tblNWOC_IDX_PC_XMAP)  as XYZ
          ON bbadger.tblcommhmo2.IDX_PLANNUMBER = XYZ.Link_Field


VC
0
Webinar: MongoDB® Index Types

Join Percona’s Senior Technical Services Engineer, Adamo Tonete as he presents “MongoDB Index Types, How, When and Where Should They be Used?” on Wednesday, July 12, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7).

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10760568
You are missing the alias for the derived table as vc suggested:

  FROM schandler.tblNWOC_IDX_PC_XMAP) t2
          ON bbadger.tblcommhmo2.IDX_PLANNUMBER = t2.tblNWOC_IDX_PC_XMAP.Link_Field
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10760570
vc (as always) has it right.
0
 
LVL 11

Expert Comment

by:vc01778
ID: 10760577
I assume you also want to select something from the derived table besides link_field ?  If so,  the column names will need to be qualified with the same alias (XYZ in this case).


VC
0
 
LVL 11

Expert Comment

by:vc01778
ID: 10760589
@acperkins


Thank you ,  but not always ;)

VC
0
 
LVL 5

Author Comment

by:Atropa
ID: 10760615
Thank you for ending my 3 hour long misery.  Now I can go to sleep in peace!  /Atropa
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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.
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…

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