创建年份为 9999 的事实 table

Creating fact table with year of 9999

我正在 oracle 中根据客户状态构建一个简单的事实 table,其中客户有一个状态,'Active' 和 'Lost' 以及他们从该状态开始的日期以及他们结束的日期。

示例 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

在这里,客户 1 处于活动状态,然后丢失了。当帐户状态为当前(截至今天)时,结束日期列为 31/12/9999。 Cust 2 从今天开始活跃

我的问题是,我怎样才能把它变成事实 table?

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

事实Table:

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

我似乎无法让 oracle 识别 9999 年任何帮助表示感谢

and 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>

或者,

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

为什么要创建 table 并将 DATE 转换为 STRING?你应该让日期保持原样。您应该仅将 TO_CHAR 用于显示目的。对于任何日期计算,让日期保持为日期。