简体   繁体   中英

Combining Data from Two Tables into One Row

I've read around quite a bit for a solution to my problem but I can't seem to get it to work. It seems like a simple problem but I'm not getting the result set I want.

I'm working on a report that needs to pull from two tables and essentially create one row of data for each employee. The file needs to be uploaded to a healthcare vendor.

Here is an example of the data

Table1: EmployeeCheckDeduction

Employee ID  Deduction Amount  Check Date
       1234             50.00   6/30/2015
       1234             50.00   7/15/2015
       4567            100.00   6/30/2015
       4567            100.00   7/15/2015
       9876             75.00   6/30/2015
       9876             75.00   7/15/2015

Table2: EmployerContribution

Employee ID  Contribution Amount  Check Date
       1234                25.00   6/30/2015 
       1234                30.00   7/15/2015
       4567                50.00   6/30/2015
       4567                60.00   7/15/2015

Part of the problem is that not every record in Table1 will have a corresponding match in Table 2. If they are maxed out on contributions, they won't receive one on that pay. What I want is a result set that looks like this:

Employee ID  Deduction Amount  Contribution Amount  Check Date
       1234             50.00                25.00   6/30/2015
       1234             50.00                30.00   7/15/2015 
       4567            100.00                50.00   6/30/2015
       4567            100.00                60.00   7/15/2015
       9876             75.00                 0.00   6/30/2015
       9876             75.00                 0.00   7/15/2015

No matter how I try and join, it's just duplicating data. I've tried using subqueries or distinct records and no matter what I try, it's not giving me what I want. I can't figure out what I'm doing wrong.

Edit. See links below for dataset results.

http://s000.tinyupload.com/index.php?file_id=01551050904538574848

http://s000.tinyupload.com/index.php?file_id=63978789937644749322

http://s000.tinyupload.com/index.php?file_id=28700836121558977952

I think part of the problem is that in the Employee Check Deduction table there is a specific deduction code that I'm pulling out. In the employer deduction table that code also exists. However, whenever I try and add the join on those 2 fields in addition to employee ID and check date, it doesn't return results from the employees who have a deduction amount in the employee check deduction table when they don't have a corresponding record in the Employer Contribution Table. I hope that helps.

select a.employee_id, a.deduction_amount, b.contribution_amount, 
a.check_date 
from employeecheckdeduction a join employercontribution b
 on a.employee_id = b.employee_id
 and a.check_date = b.check_date

Try this by renaming the columns if necessary. You should join on employed and checkdate.

The problem here is that you are dealing with TWO different things:

a) You want a FULL list of all employees who have ever had a contribution AND/OR a deduction

b) You then want to display it in one row

While there are many ways to go about this, the first thing would be two separate out those two and handle them.

Also it is not clear if you want to GROUP BY the employee ID or if you want to see a full history. If you are to look at history, the best thing would be using a FULL JOIN such as:

select coalesce(d.employee_id,c.employee_id) employee_id
     , d.deduction_amount 
     , c.contribution_amount
     , coalesce(d.deducation_date,c.contribution_date) check_date
from employee_deduction d
    full join employee_contribution c on c.employee_id = d.employee_id

where d.DED_CD = 'H' or c.MEMO_CD = 'H'

What a FULL JOIN does is include ALL records from the first table and ALL records from the joining table regardless if there are nulls in either table.

That is why you have to use a coalesce statement in the select clause to make sure you don't wind up with a null value in the result set.

Hope this works.

SELECT a.EMPLID as EmployeeID, a.AL_AMOUNT as EmployeeContribution, ISNULL(b.AL_AMOUNT, 0) AS EmployerContribution, a.CHECK_DT
FROM PS_AL_CHK_DED as a
LEFT JOIN PS_AL_CHK_MEMO as b ON a.EMPLID = b.EMPLID AND a.CHECK_DT = b.CHECK_DT
WHERE a.AL_DEDCD = 'H'
ORDER BY a.CHECK_DT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM