I have a table as follows:
year_id | loc_id | emp_id | freq
---------------------------------
1990 | LA | fred_01| 55
1990 | LA | mury_01| 34
1990 | BA | fred_01| 20
1990 | CA | mury_01| 40
1990 | MO | fred_01| 100
1990 | PR | bill_01| 500
I want everything grouped by the year and loc, but the tricky part is I want to get the sum of all "freq" at each location, then also get the sum of all the employees "freq" that worked at that location, when they're at another location (but not including original loc amount).
Desired results of first two rows:
year_id | loc_id | sum(freq) loc | sum(freq)away
------------------------------------------------
1990 LA | 89 | 160
1990 BA | 20 | 155
I can do one at a time with an INNER JOIN, but when i try to get both sums, everything gets messed up:
select t1.loc_id , t1.year_id,sum(t2.freq),sum(t3.freq) from emp_freq t1
inner join emp_freq t2
on t1.year_id=t2.year_id and t1.emp_id=t2.emp_id and t1.loc_id=t2.loc_id
inner join emp_freq t3
on t1.year_id=t3.year_id and t1.emp_id=t3.emp_id and t1.loc_id<>t3.loc_id
group by t1.loc_id,t1.year_id
order by t1.loc_id,t1.year_id
If i comment out the second inner join, the result for the grouped by loc is correct.