Solved

Union - Sub Query

Posted on 2012-04-03
7
384 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

16 Experts available now in Live!

Get 1:1 Help Now