SQL insert into select from - 插入 id 而不是数据

SQL insert into select from - insert the id instead of the data

我需要用 lds_placement table 中的数据填充我的事实 table。我已经 select 编辑了记录,下面是它的样子:

fk1_account_id | fk3_job_role_id | salary | no_of_placements |  YEAR
---------------------------------------------------------------------
     10        |        3        | 165000 |        5         |  2010
     10        |        3        | 132000 |        4         |  2011
     10        |        3        | 132000 |        4         |  2012
     20        |        2        | 990000 |        3         |  2010
     20        |        2        | 132000 |        2         |  2011
     20        |        2        | 132000 |        2         |  2012    

我想将另一个名为 time_dim 的 table 中的 time_id 插入到列 year 中,而不是实际年份本身。

time_dim table 看起来像这样:

time_id | year
---------------
   5    | 2015
   1    | 2013
   2    | 2010
   3    | 2014
   4    | 2012
   6    | 2011

我需要插入到 "year" 列实际上是:

year
 2
 6
 4
 2
 6
 4

请告诉我在 table 中插入 time_id 而不是 year 的方法。 这是我用来 select 最顶层 table.

的代码
SELECT
     fk1_account_id,
     fk3_job_role_id,
     Sum(actual_salary)                             AS salary,
     Count(1)                                       AS no_of_placements,
     MAX(EXTRACT(YEAR FROM plt_estimated_end_date)) AS year
  FROM lds_placement
 GROUP BY fk1_account_id, fk3_job_role_id, EXTRACT(YEAR FROM plt_estimated_end_date)
 ORDER BY fk1_account_id;

如果要捕获 time_dim 中不存在 year 的记录,请使用 left join。否则使用 inner_join

select t.fk1_account_id,t.fk3_job_role_id,t.salary,t.no_of_placements
,d.time_id 
from 
(SELECT fk1_account_id, fk3_job_role_id, Sum(actual_salary) as salary, Count(1) as no_of_placements, MAX(EXTRACT(YEAR FROM plt_estimated_end_date)) AS YEAR 
FROM lds_placement
GROUP BY fk1_account_id, fk3_job_role_id, EXTRACT(YEAR FROM plt_estimated_end_date)
)t
left join time_dim d
on t.year=d.year
order by t.fk1_account_id