事实表中具有不同日期的两个维度

时间:2013-12-10 03:47:30

标签: sql oracle plsql data-warehouse

我正试图解决这个问题但却无法想到任何事情。

我有5个维度,其中两个日期不同。具体来说,

TEMP_REQUEST
------------
temprequest_id
localcouncil_id
request_date    

另一张表是:

SESSION
--------
session_id
temprequest_id
session_date
status
.
.

现在我使用这样的游标填充事实:

seq NUMBER;
 v_timeid NUMBER;

Cursor c_factable is
select  

  dw_localcouncil2.localcouncil_id,
  dw_session2.session_id,
  dw_temp2.temp_id,
  dw_temprequest2.temprequest_id,
  dw_typeoftempcover2.type_of_cover_id,
  dw_session2.session_date
from  DW_LOCALCOUNCIL2, DW_SESSION2, DW_TEMP2, DW_TEMPREQUEST2, DW_TYPEOFTEMPCOVER2
where dw_temprequest2.localcouncil_id = dw_localcouncil2.localcouncil_id AND
dw_session2.temprequest_id = dw_temprequest2.temprequest_id AND
dw_session2.temp_id = dw_temp2.temp_id AND
dw_session2.type_of_cover_id = dw_typeoftempcover2.type_of_cover_id;
begin
dbms_output.put_line('it is running!');
for c_rec in c_factable loop

SELECT time_id INTO v_timeid from dw_time
where session_date = c_rec.session_date;


insert into DW_SESSIONS_FACT values(v_timeid, c_rec.localcouncil_id, c_rec.session_id, c_rec.temp_id, c_rec.temprequest_id, c_rec.type_of_cover_id);

end loop;

我的问题是事实表中的time_id字段对应于SESSION表中的session_date而不是TEMP_REQUEST表。现在,如果我想查询事实表中的查询,例如:the number of temp requests by week!我不知道如何使用事实表的当前状态来执行此操作。

非常感谢任何帮助!

预先生成并填充时间维度。包括开始日期到结束日期之间的所有日期。它包含以下列:

TIME_DIM
---------
time_id
session_date
t_day,
t_month,
t_year

1 个答案:

答案 0 :(得分:0)

您可以在事实表中拥有多个时间ID。根据@ ElectricLlam的评论

为TRANSACTION时间添加另一列
ALTER TABLE DW_SESSIONS_FACT ADD REQUEST_TIMEID int NULL

然后,保持您当前获取time_ids的方法

SELECT time_id INTO v_timeid_transaction from dw_time
where session_date = c_rec.request_date;

我不确定为什么你的time_dim表在其日期/时间字段中有一个对SESSION的显式引用。您的时间维度应该是独立的,因此任何日期字段都可以引用它。

总结:

表:

TIME_DIM
---------
time_id
d_date   --(date/timestamp datatype)
t_day
t_month
t_year

TEMP_REQUEST
------------
temprequest_id
localcouncil_id
request_date  

SESSION
--------
session_id
temprequest_id
session_date
status

DW_SESSIONS_FACT
----------------
session_timeid, 
request_timeid,
localcouncil_id, 
session_id, 
temp_id, 
temprequest_id, 
type_of_cover_id

你的etl程序:

seq NUMBER;
v_timeid_session NUMBER;
v_timeid_request NUMBER;

Cursor c_factable is
select  

  dw_localcouncil2.localcouncil_id,
  dw_session2.session_id,
  dw_temp2.temp_id,
  dw_temprequest2.temprequest_id,
  dw_typeoftempcover2.type_of_cover_id,
  dw_session2.session_date,
  dw_temprequest2.request_date
from  DW_LOCALCOUNCIL2, DW_SESSION2, DW_TEMP2, DW_TEMPREQUEST2, DW_TYPEOFTEMPCOVER2
where dw_temprequest2.localcouncil_id = dw_localcouncil2.localcouncil_id AND
dw_session2.temprequest_id = dw_temprequest2.temprequest_id AND
dw_session2.temp_id = dw_temp2.temp_id AND
dw_session2.type_of_cover_id = dw_typeoftempcover2.type_of_cover_id;
begin
dbms_output.put_line('it is running!');
for c_rec in c_factable loop

--You can do these joins directly in your c_factable query, it would be much faster
--and you could remove the need for a cursor..
SELECT time_id INTO v_timeid_session from dw_time
where d_date = c_rec.session_date;

SELECT time_id INTO v_timeid_request from dw_time
where d_date = c_rec.request_date;


insert into DW_SESSIONS_FACT values(v_timeid_session, v_timeid_request , c_rec.localcouncil_id, c_rec.session_id, c_rec.temp_id, c_rec.temprequest_id, c_rec.type_of_cover_id);

end loop;