为日期查找维度表填充加载文件的最佳方法是什么? [英] What is the best way to populate a load file for a date lookup dimension table?

查看:105
本文介绍了为日期查找维度表填充加载文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Informix 11.70.TC4:



我有一个SQL维表,用于查找日期(pk_date)并返回另一个日期(plus1,plus2或plus3_months) ),具体取决于用户选择的是 1, 2还是 3。



表架构如下:

  TABLE date_lookup 

pk_date DATE,
plus1_months DATE,
plus2_months DATE,
plus3_months DATE
);

date_lookup(pk_date)上的唯一索引;

我有一个加载文件(以竖线分隔)包含日期为2012年1月28日至03月31日-2014。



以下是加载文件的示例:

  01-28-2012 | 02-28-2012 | 03-28-2012 | 04-28-2012 | 
2012年1月29日| 2012年2月29日| 2012年3月29日| 2012年4月29日|
2012年1月30日| 2012年2月29日| 2012年3月30日| 2012年4月30日|
2012年1月31日| 2012年2月29日| 2012年3月31日| 2012年4月30日|
...
2014年3月31日| 2014年4月30日| 2014年5月31日| 2014年6月30日|

..................... ................................................... .................



编辑: Sir Jonathan的SQL语句使用 DATE(pk_date + n UNITS MONTH 在11.70.TC5上起作用了!





我从01-28生成了pk_date的加载文件-2012至12-31-2020,以及plus1,plus2和plus3_months NULL。将其加载到date_lookup表中,然后执行以下更新语句:

  UPDATE date_lookup 
SET plus1_months = DATE(pk_date + 1 UNITS MONTH),
plus2_months = DATE(pk_date + 2 UNITS MONTH),
plus3_months = DATE(pk_date + 3 UNITS MONTH);

显然,DATE()能够将pk_date转换为DATETIME,使用TC5进行数学运算新算法,并以DATE格式返回结果!



...... ................................................... .....................................



此维度表的规则是:




  • 如果pk_date在其月份中有31天,且plus1,plus2或plus3_months只有28、29或30天,则让plus1 ,则plus2或plus3等于该月的最后一天。

  • 如果pk_date的月份中有30天,而plus1,plus2或plus3的月份中有28天或29天,则让它们等于该月的最后一个有效日期,依此类推。

  • 所有其他日期都在下个月的同一天。



我的问题是:按照上述规则自动生成2014年3月31日之后的pk_dates的最佳方法是什么?我可以使用SQL脚本 sed,C程序来完成此操作吗?



编辑:我提到了 sed 因为我已经拥有超过两年的数据,并且
可以在此数据之后对其余数据建模,或者也许像 awk 这样的工具更好? / em>

解决方案

最好的技术是升级到11.70.TC5(在32位Windows上;通常是11.70。 xC5或更高版本),并使用以下表达式:

  SELECT DATE(给定日期+ n单位月)
FROM无论何处
...

DATETIME代码在11.70.xC4和11.70.xC5之间修改为根据日期概述的规则生成日期,并使用 + n UNITS MONTH 或等价符号。



这根本不需要表。显然,尽管如此,您所有的客户也必须都在11.70.xC5上。



也许您可以将开发计算机更新为11.70.xC5,然后使用此属性



如果不能将至少某人升级到11.70.xC5,则考虑将其分发给客户。



Perl脚本建议。


Informix 11.70.TC4:

I have an SQL dimension table which is used for looking up a date (pk_date) and returning another date (plus1, plus2 or plus3_months) to the client, depending on whether the user selects a "1","2" or a "3".

The table schema is as follows:

TABLE date_lookup
(
    pk_date DATE,
    plus1_months DATE,
    plus2_months DATE,
    plus3_months DATE
);

UNIQUE INDEX on date_lookup(pk_date);

I have a load file (pipe delimited) containing dates from 01-28-2012 to 03-31-2014.

The following is an example of the load file:

01-28-2012|02-28-2012|03-28-2012|04-28-2012|
01-29-2012|02-29-2012|03-29-2012|04-29-2012|
01-30-2012|02-29-2012|03-30-2012|04-30-2012|
01-31-2012|02-29-2012|03-31-2012|04-30-2012|
...
03-31-2014|04-30-2014|05-31-2014|06-30-2014|

........................................................................................

EDIT : Sir Jonathan's SQL statement using DATE(pk_date + n UNITS MONTH on 11.70.TC5 worked!

I generated a load file with pk_date's from 01-28-2012 to 12-31-2020, and plus1, plus2 & plus3_months NULL. Loaded this into date_lookup table, then executed the update statement below:

UPDATE date_lookup
   SET plus1_months = DATE(pk_date + 1 UNITS MONTH),
       plus2_months = DATE(pk_date + 2 UNITS MONTH),
       plus3_months = DATE(pk_date + 3 UNITS MONTH);

Apparently, DATE() was able to convert pk_date to DATETIME, do the math with TC5's new algorithm, and return the result in DATE format!

.........................................................................................

The rules for this dimension table are:

  • If pk_date has 31 days in its month and plus1, plus2 or plus3_months only have 28, 29, or 30 days, then let plus1, plus2 or plus3 equal the last day of that month.
  • If pk_date has 30 days in its month and plus1, plus2 or plus3 has 28 or 29 days in its month, let them equal the last valid date of those month, and so on.
  • All other dates fall on the same day of the following month.

My question is: What is the best way to automatically generate pk_dates past 03-31-2014 following the above rules? Can I accomplish this with an SQL script, "sed", C program?

EDIT: I mentioned sed because I already have more than two years worth of data and could perhaps model the rest after this data, or perhaps a tool like awk is better?

解决方案

The best technique would be to upgrade to 11.70.TC5 (on 32-bit Windows; generally to 11.70.xC5 or later) and use an expression such as:

SELECT DATE(given_date + n UNITS MONTH)
  FROM Wherever
...

The DATETIME code was modified between 11.70.xC4 and 11.70.xC5 to generate dates according to the rules you outline when the dates are as described and you use the + n UNITS MONTH or equivalent notation.

This obviates the need for a table at all. Clearly, though, all your clients would also have to be on 11.70.xC5 too.

Maybe you can update your development machine to 11.70.xC5 and then use this property to generate the data for the table on your development machine, and distribute the data to your clients.

If upgrading at least someone to 11.70.xC5 is not an option, then consider the Perl script suggestion.

这篇关于为日期查找维度表填充加载文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆