Solved

Union - Sub Query

Posted on 2012-04-03
7
379 Views
Last Modified: 2012-04-15
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
Comment
Question by:aneilg
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
damerval earned 330 total points
ID: 37802606
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
 

Author Comment

by:aneilg
ID: 37804944
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
 

Author Comment

by:aneilg
ID: 37805300
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 37805644
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
 

Author Closing Comment

by:aneilg
ID: 37805805
partly answered
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 the fundamental information of how to create a table.

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