Solved

Conditional Inner Join

Posted on 2004-04-05
8
1,514 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

13 Experts available now in Live!

Get 1:1 Help Now