AD 日期的数据仓库

发布于 2024-11-04 04:07:16 字数 961 浏览 1 评论 0原文

我们正在为世界历史数据库创建历史档案,并且需要一个引用 AD 中所有日期的日期查找表。如何为该表创建值 - 从 1AD 到 2011 年(YYYY/MM/DD)?数据库是MySQL。

问题:

  1. 我使用 Excel 预先填充日期,然后导入到 MySQL 中:YYYY/MM/DD 但 Excel 无法识别 0007、0008 等年份,因此我无法自动复制单元格生成日期。我必须手动执行此操作,这需要几天时间才能从 1AD 到 2011 年(YYYY/MM/DD)。

  2. 闰年于 1752 年引入。如果我以编程方式生成日期,如何处理 1752 年之前没有闰年的情况?它将生成错误的日期。

我的桌子:

    CREATE TABLE `dates` (
      `date_id` int(10) NOT NULL,
      `format` char(10) NOT NULL,
      `century` int(10) NOT NULL,
      `decade` int(10) NOT NULL,
      `year` int(10) NOT NULL,
      `month` int(10) NOT NULL,
      `week` int(10) NOT NULL,
      `day` int(10) NOT NULL,
      `month_year` int(10) NOT NULL,
      `week_year` int(10) NOT NULL,
      `week_month` int(10) NOT NULL,
      `day_year` int(10) NOT NULL,
      `day_month` int(10) NOT NULL,
      `day_week` int(10) NOT NULL,
      PRIMARY KEY (`date_id`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

We're creating a historic archive for a world history database and we need a date lookup table which references all dates in AD. How to go about creating the values for this table - from 1AD to 2011 as YYYY/MM/DD? Database is MySQL.

Problems:

  1. I'm using Excel to pre-populate the dates, then import into MySQL as: YYYY/MM/DD but Excel doesn't recognize years like 0007, 0008, etc so I can't auto-copy cells to generate dates. I have to manually do it and this will take days to go from 1AD to year 2011 as YYYY/MM/DD.

  2. Leap years were introduced on 1752. If I programmatically generates dates how do I handle cases prior to 1752 with no leap years? It will generate wrong dates.

My table:

    CREATE TABLE `dates` (
      `date_id` int(10) NOT NULL,
      `format` char(10) NOT NULL,
      `century` int(10) NOT NULL,
      `decade` int(10) NOT NULL,
      `year` int(10) NOT NULL,
      `month` int(10) NOT NULL,
      `week` int(10) NOT NULL,
      `day` int(10) NOT NULL,
      `month_year` int(10) NOT NULL,
      `week_year` int(10) NOT NULL,
      `week_month` int(10) NOT NULL,
      `day_year` int(10) NOT NULL,
      `day_month` int(10) NOT NULL,
      `day_week` int(10) NOT NULL,
      PRIMARY KEY (`date_id`)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2024-11-11 04:07:16

使用类似这样的 SQL(使用我自己的表结构,而不是您的表结构):

DROP TABLE IF EXISTS time_dimension;
CREATE TABLE time_dimension (
        id                      INTEGER PRIMARY KEY,  -- year*10000+month*100+day
        db_date                 DATE NOT NULL,
        year                    INTEGER NOT NULL,
        month                   INTEGER NOT NULL, -- 1 to 12
        day                     INTEGER NOT NULL, -- 1 to 31
        quarter                 INTEGER NOT NULL, -- 1 to 4
        week                    INTEGER NOT NULL, -- 1 to 52/53
        day_name                VARCHAR(9) NOT NULL, -- 'Monday', 'Tuesday'...
        month_name              VARCHAR(9) NOT NULL, -- 'January', 'February'...
        holiday_flag            CHAR(1) DEFAULT 'f' CHECK (holiday_flag in ('t', 'f')),
        weekend_flag            CHAR(1) DEFAULT 'f' CHECK (weekday_flag in ('t', 'f')),
        event                   VARCHAR(50),
        UNIQUE td_ymd_idx (year,month,day),
        UNIQUE td_dbdate_idx (db_date)

) Engine=MyISAM;

DROP PROCEDURE IF EXISTS fill_date_dimension;
DELIMITER //
CREATE PROCEDURE fill_date_dimension(IN startdate DATE,IN stopdate DATE)
BEGIN
    DECLARE currentdate DATE;
    SET currentdate = startdate;
    WHILE currentdate < stopdate DO
        INSERT INTO time_dimension VALUES (
                        YEAR(currentdate)*10000+MONTH(currentdate)*100 + DAY(currentdate),
                        currentdate,
                        YEAR(currentdate),
                        MONTH(currentdate),
                        DAY(currentdate),
                        QUARTER(currentdate),
                        WEEKOFYEAR(currentdate),
                        DATE_FORMAT(currentdate,'%W'),
                        DATE_FORMAT(currentdate,'%M'),
                        'f',
                        CASE DAYOFWEEK(currentdate) WHEN 1 THEN 't' WHEN 7 then 't' ELSE 'f' END,
                        NULL);
        SET currentdate = ADDDATE(currentdate,INTERVAL 1 DAY);
    END WHILE;
END
//
DELIMITER ;

TRUNCATE TABLE time_dimension;

CALL fill_date_dimension('1-01-01','2015-01-01');
OPTIMIZE TABLE time_dimension;

Use something like this SQL (using my own table structure, not the one you had though):

DROP TABLE IF EXISTS time_dimension;
CREATE TABLE time_dimension (
        id                      INTEGER PRIMARY KEY,  -- year*10000+month*100+day
        db_date                 DATE NOT NULL,
        year                    INTEGER NOT NULL,
        month                   INTEGER NOT NULL, -- 1 to 12
        day                     INTEGER NOT NULL, -- 1 to 31
        quarter                 INTEGER NOT NULL, -- 1 to 4
        week                    INTEGER NOT NULL, -- 1 to 52/53
        day_name                VARCHAR(9) NOT NULL, -- 'Monday', 'Tuesday'...
        month_name              VARCHAR(9) NOT NULL, -- 'January', 'February'...
        holiday_flag            CHAR(1) DEFAULT 'f' CHECK (holiday_flag in ('t', 'f')),
        weekend_flag            CHAR(1) DEFAULT 'f' CHECK (weekday_flag in ('t', 'f')),
        event                   VARCHAR(50),
        UNIQUE td_ymd_idx (year,month,day),
        UNIQUE td_dbdate_idx (db_date)

) Engine=MyISAM;

DROP PROCEDURE IF EXISTS fill_date_dimension;
DELIMITER //
CREATE PROCEDURE fill_date_dimension(IN startdate DATE,IN stopdate DATE)
BEGIN
    DECLARE currentdate DATE;
    SET currentdate = startdate;
    WHILE currentdate < stopdate DO
        INSERT INTO time_dimension VALUES (
                        YEAR(currentdate)*10000+MONTH(currentdate)*100 + DAY(currentdate),
                        currentdate,
                        YEAR(currentdate),
                        MONTH(currentdate),
                        DAY(currentdate),
                        QUARTER(currentdate),
                        WEEKOFYEAR(currentdate),
                        DATE_FORMAT(currentdate,'%W'),
                        DATE_FORMAT(currentdate,'%M'),
                        'f',
                        CASE DAYOFWEEK(currentdate) WHEN 1 THEN 't' WHEN 7 then 't' ELSE 'f' END,
                        NULL);
        SET currentdate = ADDDATE(currentdate,INTERVAL 1 DAY);
    END WHILE;
END
//
DELIMITER ;

TRUNCATE TABLE time_dimension;

CALL fill_date_dimension('1-01-01','2015-01-01');
OPTIMIZE TABLE time_dimension;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文