Link to home
Start Free TrialLog in
Avatar of aneilg
aneilgFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Union Joins

I have three unions [1011], [1011], [1112].

With the unions you need to have the same numhber of rows in each select, which is fine.

The additional select in each union is      
,[Primary Diagnosis Code],LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC

But my question is I only want to do the joins on the last union. But this causes a problem because Msg 4104, Level 16, State 1, Line 119
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
Msg 4104, Level 16, State 1, Line 279
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.

Which means the join need to be in each union.



SELECT

     
      [Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,[Secondary Diagnosis Code 2]
      ,[Secondary Diagnosis Code 3]
      ,[Secondary Diagnosis Code 4]


           
FROM  [0910]


      UNION ALL

     
SELECT  

     
      [Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,[Secondary Diagnosis Code 2]
      ,[Secondary Diagnosis Code 3]
      ,[Secondary Diagnosis Code 4]

     
FROM [1011]

      UNION ALL

SELECT

      [Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
     
      ,[Secondary Diagnosis Code 1]
      ,[Secondary Diagnosis Code 2]
      ,[Secondary Diagnosis Code 3]
      ,[Secondary Diagnosis Code 4]
 
 
     
FROM      [1112]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
                  ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
                  ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
                  ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
                  ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
                  ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]


GO
Avatar of ingriT
ingriT
Flag of Netherlands image

Try using the UNION keyword instead of the UNION ALL keyword.
ASKER CERTIFIED SOLUTION
Avatar of gplana
gplana
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

this is an example of the select working.

my question is, is it possible not to have the joins in each union, only having the join on [1112]. but the problem is

Msg 4104, Level 16, State 1, Line 119
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
ect.

the query as it stands need the joins in each union. is it possible to only have the join on
the last query

SELECT

      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

            
FROM  [0910]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
            --      ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
            --      ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
            --      ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
            --      ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
            --      ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]

      UNION

      
SELECT  

     
      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

      
      
FROM [1011]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
            --      ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
            --      ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
            --      ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
            --      ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
            --      ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]


      UNION

SELECT



      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC

      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

      
FROM      [1112]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
                  ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
                  ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
                  ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
                  ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
                  ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]
Yes, just try to remove joins now, as I have replaced the field by an empty string on the select.
Avatar of aneilg

ASKER

ok i'll give it a go.

i was told this approcah is possible.
Select fieldNames from (

10/11 query

Union all

11/12 query

) as subQuery
Then do all the joins here
Yes, this is also possible, but isn't it better the solution I give ?

Or maybe I misunderstood the goal ?
Avatar of aneilg

ASKER

yeah but he is Adamant I do it his way.


but if i do

SELECT * FROM (

SELECT

      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

           
FROM  [0910]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
            --      ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
            --      ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
            --      ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
            --      ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
            --      ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]

      UNION

     
SELECT  

     
      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC
     
      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

     
     
FROM [1011]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
            --      ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
            --      ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
            --      ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
            --      ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

            --      LEFT OUTER JOIN
            --[LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
            --      ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]


      UNION

SELECT



      ,[Primary Diagnosis Code]
      ,LEFT([Primary Diagnosis Code],5) AS PRIMARY_DIAGNOSIS_ICD10
        ,ICD_PRIM_DIAG.ICD104NM AS PRIMARY_DIAGNOSIS_DESC

      ,[Secondary Diagnosis Code 1]
      ,LEFT([Secondary Diagnosis Code 1],5) AS [1ST_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC1_DIAG.ICD104NM AS SEC_DIAG1ST_DESC
     
      ,[Secondary Diagnosis Code 2]
      ,LEFT([Secondary Diagnosis Code 2],5) AS [2ND_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC2_DIAG.ICD104NM AS SEC_DIAG2ND_DESC
     
      ,[Secondary Diagnosis Code 3]
      ,left([Secondary Diagnosis Code 3],5) AS [3RD_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC3_DIAG.ICD104NM AS SEC_DIAG3RD_DESC
     
      ,[Secondary Diagnosis Code 4]
      ,left([Secondary Diagnosis Code 4],5) AS [4TH_SEC_DIAGNOSIS_ICD10]
        ,ICD_SEC4_DIAG.ICD104NM AS SEC_DIAG4TH_DESC
     

     
FROM      [1112]

) as g


                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_PRIM_DIAG
                  ON ICD_PRIM_DIAG.ICD104CD = LEFT([Primary Diagnosis Code],4)

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC1_DIAG
                  ON ICD_SEC1_DIAG.ICD104CD = [Secondary Diagnosis Code 1]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC2_DIAG
                  ON ICD_SEC2_DIAG.ICD104CD = [Secondary Diagnosis Code 2]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC3_DIAG
                  ON ICD_SEC3_DIAG.ICD104CD = [Secondary Diagnosis Code 3]

                  LEFT OUTER JOIN
            [LookUp].[dbo].[tbl_ICD10] AS ICD_SEC4_DIAG
                  ON ICD_SEC4_DIAG.ICD104CD = [Secondary Diagnosis Code 4]


i get Msg 4104, Level 16, State 1, Line 121
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
Ok, in this case try the other approach:

SELECT .... FROM (
                 SELECT ... (without joins)
                 UNION ALL
                 SELECT ... (without joins)
                 UNION ALL
                 SELECT ... (without joins)
) and make the joins here
Avatar of aneilg

ASKER

thanks for your help,

i've done

SELECT * FROM (

) as g

joins

but get Msg 4104, Level 16, State 1, Line 121
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
Msg 4104, Level 16, State 1, Line 125

is this appracah possible.
This is because you don't have the ICD_PRIM_DIAG table on your SELECT or because field ICD104NM is not a valid field on ICD_PRIM_DIAG table
Avatar of aneilg

ASKER

thanks.