我对postgres和DW还不熟悉,我必须设计一个DATE Dimension,如book所示。我在网上看到了很多地方,但到目前为止还没有成功,能否解释一下如何填充'Fiscal Week', 'Fiscal Month', 'Fiscal Half year'
谢谢你

最佳答案

我想您需要10年日期维度表,其中所有列都显示在图2.4中(基于Google Books)。签入文档:
模板模式的格式化函数to_char
提取功能Table 9-21
生成date_part的行序列
为了在10年内得到所有的时间,你可以写:

SELECT generate_series('2001-01-01'::date, '2010-12-31'::date, '1 day') AS day;

SELECT count(*) FROM generate_series('2001-01-01'::date, '2010-12-31'::date, '1 day');
 count
-------
  3652
(1 row)

根据图2.5创建表,例如:
DROP TABLE IF EXISTS "Date Dimension";
CREATE TABLE "Date Dimension"
(
    "Date Key" serial,
    "Date" date,
    "Full Day Description" text,
    "Day Of Week" text,
    "Calendar Month" text,
    "Calendar Year" integer,
    "Fiscal Year Month" text,
    "Holiday Indicator" text,
    "Weekday Indicator" text
);

插入命令:
INSERT INTO "Date Dimension"
    ("Date", "Full Day Description", "Day Of Week", "Calendar Month",
    "Calendar Year", "Fiscal Year Month", "Holiday Indicator",
    "Weekday Indicator")
SELECT
    day,
    rtrim(to_char(day, 'Month')) || to_char(day, ' DD, YYYY'),
    to_char(day, 'Day'),
    rtrim(to_char(day, 'Month')),
    date_part('year', day),
    'F' || to_char(day, 'YYYY-MM'),
    '', --omitting (trivial 'Holiday'/'Non-Holiday, but how to get this ??),
    CASE
        WHEN date_part('isodow', day) IN (6, 7) THEN 'Weekend'
        ELSE 'Weekday'
    END
FROM
    generate_series('2001-01-01'::date, '2010-12-31'::date, '1 day') day;

我希望这能给你一些框架和起点。

10-02 05:48