Solved

Union - Sub Query

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2003 query lost it's only join 7 27
Separate 2 comma delimited columns into separate rows 2 31
Increment column based of a FK 8 21
SQL server vNext 18 24
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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

770 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