0

I have a problem , got three Tables

Table A 

ID        Employee    
1         1    
2         2
3         3


Table B
Id        EMployee        HoursWorked         HoursCode
1          1                 10               Basic Hours
2          1                 20               Holiday Pay
3          2                 10               Basic hours
4          2                 15               OverTime

Table C 
ID        Employee         Payments            PayCode
1           1                 100              Bonus
2           2                 150              Bonus
3           2                 250              Student Loan

I want to get the records out of these table in minimum lines , so i can have one line which says

id        Employee          Hour               HoursCode       Payments      PayCode
1          1                 10                Basic Hours     100           Bonus     
2          1                 20                Holiday Pay      null         null
3          2                 10                basic hours      150          Bonus 
4          2                 15                 OverTime        250          Student loan

I have spent ages trying to get it ... But dont get the Null in the 2nd line it comes out with 100 Bonus in second line for employee 1

is there way i can do this Please Help

3
  • 1
    The problem is that there is no link between table B and table C... example, in your result you show that the 100$ for employee 1 was paid for his "Basic Hours" work... but how do we know that... where in your data do we know that the 100$ 'Payment' wasn't for his 'Holiday Pay'? Commented Jan 20, 2010 at 10:32
  • 1
    How are table B and C related? Commented Jan 20, 2010 at 10:32
  • B and C are related only Via the Employee Number. Commented Jan 20, 2010 at 11:05

1 Answer 1

3
WITH    bn AS
        (
        SELECT  *, ROW_NUMBER() OVER (PARTITION BY employee ORDER BY id) AS rn
        FROM    b
        ),
        cn AS
        (
        SELECT  *, ROW_NUMBER() OVER (PARTITION BY employee ORDER BY id) AS rn
        FROM    c
        )
SELECT  *
FROM    bn
FULL JOIN
        cn
ON      bn.employee = cn.employee
        AND bn.rn = cn.rn
ORDER BY
        COALESCE(bn.employee, cn.employee), COALESCE(bn.rn, cn.rn)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.