创建年份为9999的事实表

时间:2015-02-13 02:32:48

标签: oracle schema data-warehouse star-schema

我根据客户状态在oracle中构建一个简单的事实表,其中客户具有状态,“活跃”状态。并且'迷失'以及他们从该状态开始的日期以及他们结束的日期。

样本3行将是;

CustID | status | date_start | date_end   
---------------------------------------
  1    | active |   1/1/13   |  1/12/14  
  1    | lost   | 1/12/14    | 31/12/9999  
  2    |active  | 1/12/14    | 31/12/9999

在这里,cust 1活跃然后丢失了。当帐户状态为当前(截至今天)时,结束日期列为31/12/9999。 Cust 2自今天起生效

我的问题是,如何将其纳入事实表?

CREATE TABLE temp AS 
SELECT  CS.contract_status_id , to_char(ASH.Contract_Status_Start, 'DD/MM/YYYY') AS cust_status_start_date, to_char(ASH.CONTRACT_STATUS_END, 'DD/MM/YYYY') As cust_status_end_date
FROM account_status_history ASH,   
customer_status_dim CS
WHERE ASH.contract_status = CS.contract_status

事实表:

CREATE TABLE customer_status_fact AS
SELECT T.cust_status_start_date, T.cust_status_end_date, T.contract_status_id,
count(T.contract_status_id) AS TOTAL_ACCOUNTS
FROM temp T
GROUP BY T.cust_status_start_date, T.cust_status_end_date, T.contract_status_id

测试它;

select sum(F.TOTAL_ACCOUNTS), CS.contract_status_txt 
from customer_status_fact F, customer_status_dim CS

where F.contract_status_id = CS.contract_status_id
and F.cust_status_start_date <= sysdate 
and F.cust_status_end_date = '31/12/9999'
group by CS.contract_status_txt

我似乎无法让甲骨文认出9999年的任何帮助表示赞赏

1 个答案:

答案 0 :(得分:3)

  

和F.cust_status_end_date = '31 / 12/9999'

'31/12/9999'不是 DATE ,它是一个用单引号括起来的字符串。您必须使用 TO_DATE 将其明确转换为DATE。

例如,

SQL> alter session set nls_date_format='DD/MM/YYYY HH24:MI:SS';

Session altered.

SQL> SELECT to_date('31/12/9999 23:59:59','DD/MM/YYYY HH24:MI:SS') FROM dual;

TO_DATE('31/12/9999
-------------------
31/12/9999 23:59:59

SQL>

OR,

SQL> SELECT to_date(5373484, 'J') + (1 - 1/24/60/60) FROM dual;

TO_DATE(5373484,'J'
-------------------
31/12/9999 23:59:59

SQL>
  

CREATE TABLE temp AS SELECT CS.contract_status_id,   to_char(ASH.Contract_Status_Start,'DD / MM / YYYY')AS   cust_status_start_date,to_char(ASH.CONTRACT_STATUS_END,'DD / MM / YYYY')   as cust_status_end_date

为什么要创建一个将DATE转换为STRING的表?你应该让日期保持不变。您应该仅将 TO_CHAR 用于显示目的。对于任何日期计算,请将日期保留为日期。