Solved

Union - Sub Query

Posted on 2012-04-03
7
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
Philippe 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 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