Solved

Conditional Inner Join

Posted on 2004-04-05
8
1,515 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
  • 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
 
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now