Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conditional Inner Join

Posted on 2004-04-05
8
Medium Priority
?
1,523 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 2000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

722 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