Solved

Conditional Inner Join

Posted on 2004-04-05
8
1,517 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL replication over high latency link 10 61
Database Integrity 1 51
SQLCMD Syntax 2 17
Shrink multiple databases at once 4 31
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.

821 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