Solved

SQl Query

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

Accepted Solution

by:
waltersnowslinarnold earned 500 total points
Comment Utility
The problem is not with JOINs, you can perform JOIN in any part of a UNION. but the SELECT clause in the last has column without proper alias name, which prompts the error. I mean to say is, The below columns you mentioned in the last SELECT clause may be available in multiple tables in the JOIN tables, so please restrict it by saying alias names before each column. That would solve the issue you facing.

  [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]
0
 

Author Comment

by:aneilg
Comment Utility
yeah, the problem is i have been told to be it this way.

Select fieldNames from (

10/11 query

Union all

11/12 query

) as subQuery

but i get Msg 4104, Level 16, State 1, Line 121
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
Comment Utility
You should actually give the alias name "PRIMARY_DIAGNOSIS_DESC" provided in the outer SELECT Clause. This should solve the problem.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:aneilg
Comment Utility
sorry to be a pain but can you give me an example.

i've craeted a sub query.

SELECT * FROM (


) as g

then done my joins but get.

Msg 4104, Level 16, State 1, Line 121
The multi-part identifier "ICD_PRIM_DIAG.ICD104NM" could not be bound.
Msg 4104, Level 16, State 1, Line 125
0
 
LVL 7

Expert Comment

by:waltersnowslinarnold
Comment Utility
A sample query for little more clarity

SELECT
subQuery.col1, subQuery.col2
FROM
(
 SELECT col1,col2, col3
 FROM tablename
) subQuery
0
 
LVL 6

Expert Comment

by:Patrick Tallarico
Comment Utility
is the field ICD104NM part of the table's constraints or clustered key(s)?
0
 

Author Closing Comment

by:aneilg
Comment Utility
thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now