Solved

INSERT INTO SELECT JOIN THING

Posted on 2016-11-10
2
58 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 500 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

729 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