?
Solved

INSERT INTO SELECT JOIN THING

Posted on 2016-11-10
2
Medium Priority
?
64 Views
Last Modified: 2016-11-11
Hi there!

I'm working in MSSQL Server 2008 R2

I have a table "AllQuarters" with a list of distinct employee numbers [EMPLOYEE] and fields for quarterly wages for those employees like so:
---------------------------------------------------------------------------------------------------------------------------------------------
EMPLOYEE | Q1_GROSS_WAGES | Q2_GROSS_WAGES | Q3_GROSS_WAGES | Q4_GROSS_WAGES |
---------------------------------------------------------------------------------------------------------------------------------------------

I also have 4 tables (Q1 - Q4) that have the employee number [EMPLOYEE] and the gross wages [GROSS_WAGES] for the employee for that quarter.

I'm trying to join the Q1 - Q4 tables to the AllQuarters table on [EMPLOYEE] and insert the [GROSS_WAGES] from the appropriate quarter into its respective [QX_GROSS_WAGES] field in AllQuarters.

Not all employees exist in all Q1 - Q4 tables.  

I've tried INSERT INTO SELECT with a JOIN but my results end up with a bunch of NULL values.  Not sure what I'm doing wrong.  Any help will be greatly appreciated.  

TIA!
0
Comment
Question by:ttist25
[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
2 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 41882531
INSERT INTO AllQuarters ( EMPLOYEE, Q1_GROSS_WAGES, Q2_GROSS_WAGES, Q3_GROSS_WAGES, Q4_GROSS_WAGES )
SELECT COALESCE(Q1.EMPLOYEE, Q2.EMPLOYEE, Q3.EMPLOYEE, Q4.EMPLOYEE) AS EMPLOYEE,
    Q1.GROSS_WAGES, Q2.GROSS_WAGES, Q3.GROSS_WAGES, Q4.GROSS_WAGES  
FROM Q1
FULL OUTER JOIN Q2 ON Q2.EMPLOYEE = Q1.EMPLOYEE
FULL OUTER JOIN Q3 ON Q3.EMPLOYEE IN (Q1.EMPLOYEE, Q2.EMPLOYEE)
FULL OUTER JOIN Q4 ON Q4.EMPLOYEE IN (Q1.EMPLOYEE, Q2.EMPLOYEE, Q3.EMPLOYEE)
0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 41883856
Thanks again Scott!
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

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