最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

mysql中创建时间维度

来源:动视网 责编:小采 时间:2020-11-09 07:41:22
文档

mysql中创建时间维度

mysql中创建时间维度:Small-numbers table DROP TABLE IF EXISTS numbers_small; CREATE TABLE numbers_small (number INT); INSERT INTO numbers_small VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); Main-numbers table DROP TABLE IF EXISTS numbers; CREATE TABLE number
推荐度:
导读mysql中创建时间维度:Small-numbers table DROP TABLE IF EXISTS numbers_small; CREATE TABLE numbers_small (number INT); INSERT INTO numbers_small VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); Main-numbers table DROP TABLE IF EXISTS numbers; CREATE TABLE number


Small-numbers table DROP TABLE IF EXISTS numbers_small; CREATE TABLE numbers_small (number INT); INSERT INTO numbers_small VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); Main-numbers table DROP TABLE IF EXISTS numbers; CREATE TABLE number


  • Small-numbers table
  • DROP TABLE IF EXISTS numbers_small;
    CREATE TABLE numbers_small (number INT);
    INSERT INTO numbers_small VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

  • Main-numbers table
  • DROP TABLE IF EXISTS numbers;
    CREATE TABLE numbers (number BIGINT);
    INSERT INTO numbers
    SELECT thousands.number * 1000 + hundreds.number * 100 + tens.number * 10 + ones.number
    FROM numbers_small thousands, numbers_small hundreds, numbers_small tens, numbers_small ones
    LIMIT 1000000;

  • Create Date Dimension table
  • DROP TABLE IF EXISTS Dates_D;
    CREATE TABLE Dates_D (
    date_id BIGINT PRIMARY KEY,
    date DATE NOT NULL,
    day CHAR(10),
    day_of_week INT,
    day_of_month INT,
    day_of_year INT,
    previous_day date NOT NULL default '0000-00-00',
    next_day date NOT NULL default '0000-00-00',
    weekend CHAR(10) NOT NULL DEFAULT "Weekday",
    week_of_year CHAR(2),
    month CHAR(10),
    month_of_year CHAR(2),
    quarter_of_year INT,
    year INT,
    UNIQUE KEY `date` (`date`));

  • First populate with ids and Date
  • INSERT INTO Dates_D (date_id, date)
    SELECT number, DATE_ADD( '2010-01-01', INTERVAL number DAY )
    FROM numbers
    WHERE DATE_ADD( '2010-01-01', INTERVAL number DAY ) BETWEEN '2010-01-01' AND '2010-12-31'
    ORDER BY number;

    Change year start and end to match your needs. The above sql creates records for year 2010.

  • Update other columns based on the date.
  • UPDATE Dates_D SET
    day = DATE_FORMAT( date, "%W" ),
    day_of_week = DAYOFWEEK(date),
    day_of_month = DATE_FORMAT( date, "%d" ),
    day_of_year = DATE_FORMAT( date, "%j" ),
    previous_day = DATE_ADD(date, INTERVAL -1 DAY),
    next_day = DATE_ADD(date, INTERVAL 1 DAY),
    weekend = IF( DATE_FORMAT( date, "%W" ) IN ('Saturday','Sunday'), 'Weekend', 'Weekday'),
    week_of_year = DATE_FORMAT( date, "%V" ),
    month = DATE_FORMAT( date, "%M"),
    month_of_year = DATE_FORMAT( date, "%m"),
    quarter_of_year = QUARTER(date),
    year = DATE_FORMAT( date, "%Y" );

    文档

    mysql中创建时间维度

    mysql中创建时间维度:Small-numbers table DROP TABLE IF EXISTS numbers_small; CREATE TABLE numbers_small (number INT); INSERT INTO numbers_small VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); Main-numbers table DROP TABLE IF EXISTS numbers; CREATE TABLE number
    推荐度:
    标签: 创建 时间 mysql
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top