• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

Union - Sub Query

i need to do the following

then can you make it so that you only link the lookups once rather than on each of the tables which are unioned together i.e. in a subquery like


Select fieldNames from (

[CBS2] query

Union all

[CBS2] query

) as subQuery
Then do all the joins here

heres my query






      
SELECT  

      [Primary tbl Code]
      ,LEFT([Primary tbl Code],5) AS PRIMARY
        ,PRIM AS PRIMARY_tbl_DESC
     
      ,[Secondary tbl Code 1]
      ,[Secondary tbl Code 2]
      ,[Secondary tbl Code 3]
      ,[Secondary tbl Code 4]

   
      
FROM [CBS2]

      UNION ALL

SELECT



      [Primary tbl Code]
      ,LEFT([Primary tbl Code],5) AS PRIMARY
        ,ICD_PRIM_ AS PRIMARY_tbl_DESC
           
      ,[Secondary tbl Code 1]
      ,[Secondary tbl Code 2]
      ,[Secondary tbl Code 3]
      ,[Secondary tbl Code 4]


      
FROM      [CBS3]

----------------------------------------------

select

left([Primary tbl Code],5) AS PRIMARY_tbl_ICD10,
ICD_PRIM_ AS PRIMARY_tbl_DESC,

left([Secondary tbl Code 1],5) AS [1ST_SEC_tbl_ICD10],
ICD_SEC1_ AS SEC_DIAG1ST_DESC,

left([Secondary tbl Code 2],5) AS [2ND_SEC_tbl_ICD10],
ICD_SEC2_ AS SEC_DIAG2ND_DESC,

left([Secondary tbl Code 3],5) AS [3RD_SEC_tbl_ICD10],
ICD_SEC3_AS SEC_DIAG3RD_DESC,

left([Secondary tbl Code 4],5) AS [4TH_SEC_tbl_ICD10],
ICD_SEC4_ AS SEC_DIAG4TH_DESC



FROM        
[CBS_DW]


LEFT OUTER JOIN
[LookUp].[dbo].[tbl_] AS ICD_ ON ICD_PRIM_ = left([Primary tbl Code],4)

LEFT OUTER JOIN
[LookUp].[dbo].[tbl_] AS ICD_ ON ICD_SEC1_ = [Secondary tbl Code 1]

LEFT OUTER JOIN
[LookUp].[dbo].[tbl_] AS ICD_ ON ICD_SEC2_ = [Secondary tbl Code 2]

LEFT OUTER JOIN
[LookUp].[dbo.tbl_] AS ICD_ ON ICD_SEC3_ = [Secondary tbl Code 3]

LEFT OUTER JOIN
[LookUp].[dbo.tbl_] AS ICD_ ON ICD_SEC4_ = [Secondary tbl Code 4]






GO
0
aneilg
Asked:
aneilg
  • 3
1 Solution
 
Philippe DamervalSenior Analyst ProgrammerCommented:
Aneilg,
Your question is very hard to understand. I think you are trying to figure out a way to create a subquery from a union so you can join the result to another table. One way to do this if you are using SQL server 2005 or later is to use a CTE (common table expression). This is done using the following syntax:

WITH MyCTE as (
<UNION statement>
)
SELECT ..... FROM table INNER JOIN MyCTE ON ....

I hope this helps. If you need further help, please try to ask your question more clearly. Thank you!

Philippe
0
 
aneilgAuthor Commented:
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
0
 
aneilgAuthor Commented:
i've been told to do the query like this.

Select fieldNames from (

10/11 query

Union all

11/12 query

) as subQuery
Then do all the joins here
0
 
Anthony PerkinsCommented:
With the unions you need to have the same numhber of rows in each select, which is fine.
Huh?  I suspect you mean columns and not rows.
0
 
aneilgAuthor Commented:
partly answered
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now