3 Way join with different column names.

Hi,
I have 3 files each having 2 cols
e.g
File1
empid deptid
1 100
2 200
3 200
4 100
5 300
File2
empid salary
1 10
2 20
3 30
4 40
5 50
6 60
File3
deptid deptname
100 A
200 B
300 C
400 D
in the output i want total amt spent on each dept i.e A,B,C,D which would be a sum in rollup for each dept(100,200,300,400) salary of each employee.
So here i will have to join
File1.empid =File2.empid (which gives salaries) AND
File1.deptid = File3.deptid (which gives dept name)
So... in o/p i want dept_name and amt_spent_each_dept
My solution has
File1 ---
File2 ------[ JOIN] ---[Rollup] --- [File4 as output]
File3 ---
But what condition can i have in JOIN component to join these 3 tables ?

Questions by vital_parlsey   answers by vital_parlsey

Showing Answers 1 - 15 of 15 Answers

I believe what you are looking for is a query like this:

select f3.deptname dept_name, sum(isnull(f2.salary,0)) amt_spent_each_dept
from file3 f3 left join file1 f1 on (f3.deptid = f1.deptid)
left join file2 f2 on (f1.empid = f2.empid)
group by f3.deptname
order by f3.deptname

  Was this answer useful?  Yes

Thanks for the reply...but i want to do this in ab initio .....could you be a bit more specific...
i will explain the question again..

Code
  1. Hi,

  2.  

  3. I have 3 files  each having 2 cols

  4. e.g

  5. File1

  6. empid       deptid

  7. 1                 100  

  8. 2                 200

  9. 3                 200

  10. 4                 100

  11. 5                 300

  12.  

  13. File2

  14. empid       salary

  15. 1                 10  

  16. 2                  20

  17. 3                  30

  18. 4                  40

  19. 5                  50

  20. 6                  60

  21.  

  22. File3

  23. deptid      deptname

  24. 100           A

  25. 200           B

  26. 300           C

  27. 400           D

  28.  

  29. in the output i want total amt spent on each dept i.e A,B,C,D which would be a sum in rollup for each dept(100,200,300,400) salary of each employee.

  30.  

  31. So here i will have to join

  32. File1.empid =File2.empid  (which gives salaries) AND

  33. File1.deptid = File3.deptid (which gives dept name)

  34.  

  35. So... in o/p i want dept_name and amt_spent_each_dept

  36.  

  37. My solution has

  38. File1 ---

  39. File2   ------[ JOIN] ---[Rollup] --- [File4 as output]

  40. File3 ---

  41.  

  42. But what condition can i have in JOIN component to join these 3 tables ?

  43.  

  Was this answer useful?  Yes

Setup the join component- Fine .. I will answer this myself You don't need to mention a key here ..neither key nor override key So in the transform you have find the columns u need to match and put a condition for them in each column. so u may get a cartesian product and after that check it with a filter by expression where you don't need null (check this with is_valid or is_null) and then fwd it to sort where key is dept_id and then to roll up where key is again dept_id and roll up on sal So here you get your result for each dept expense.

  Was this answer useful?  Yes

Malini

  • Apr 3rd, 2012
 

Code
  1.   SELECT C.DEPTNAME,sum(B.SALARY) FROM FILE1 A ,FILE2 B ,FILE3 C

  2.   WHERE A.EMPLID=B.EMPLID AND A.DEPTID=C.DEPTID

  3.   GROUP BY C.DEPTNAME

  Was this answer useful?  Yes

PLMS

  • Mar 29th, 2013
 

Step 1: File1 - left outer Join {Key- empid} with -File2

Step 2: The above output flow should be sort{PBKS-if multifile} based on key empid

Step 3: The step 2s o/p flow to be left outer join {key: deptid} with -File 3

Step 4: In Rolloup-sum the the salary and map it to total_amt based on key deptid.

Step 5: FIle 4 will be ready now with required o/p (deptid,dept_name & total_amt)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions