数据仓库的日历表

发布于 2024-07-29 08:44:24 字数 4188 浏览 3 评论 0原文

对于我的数据仓库,我创建一个日历表,如下所示:

SET NOCOUNT ON

DROP Table dbo.Calendar
GO
Create Table dbo.Calendar
(
    CalendarId              Integer NOT NULL,
    DateValue               Date    NOT NULL,
    DayNumberOfWeek         Integer NOT NULL,
    NameOfDay               VarChar (10) NOT NULL,
    NameOfMonth             VarChar (10) NOT NULL,
    WeekOfYear              Integer NOT NULL,
    JulianDay               Integer NOT NULL,
    USAIsBankHoliday        Bit     NOT NULL,
    USADayName              VarChar (100) NULL,
)
ALTER TABLE dbo.Calendar ADD CONSTRAINT
    DF_Calendar_USAIsBankHoliday DEFAULT 0 FOR USAIsBankHoliday
GO
ALTER TABLE dbo.Calendar ADD CONSTRAINT
    DF_Calendar_USADayName DEFAULT '' FOR USADayName
GO

Declare @StartDate  DateTime = '01/01/2000'
Declare @EndDate    DateTime = '01/01/2020'

While @StartDate < @EndDate
Begin
    INSERT INTO dbo.Calendar 
    (
        CalendarId, 
        DateValue, 
        WeekOfYear,
        DayNumberOfWeek,
        NameOfDay,
        NameOfMonth,
        JulianDay
    )
    Values 
    (
        YEAR (@StartDate) * 10000 + MONTH (@StartDate) * 100 + Day (@StartDate), --CalendarId
        @StartDate,                 -- DateValue
        DATEPART (ww, @StartDate),  -- WeekOfYear
        DATEPART (dw, @StartDate),  -- DayNumberOfWeek
        DATENAME (dw, @StartDate),  -- NameOfDay
        DATENAME (M, @StartDate),   -- NameOfMonth
        DATEPART (dy, @StartDate)   -- JulianDay
    )

    Set @StartDate += 1
End

--=========================== Weekends
-- saturday and sunday
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Weekend, ' WHERE DayNumberOfWeek IN (1, 7) 


--=========================== Bank Holidays
-- new years day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'New Year''s Day, '  WHERE (CalendarId % 2000) IN (101)

-- memorial day (last Monday in May)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Memorial Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT MAX (CalendarId)
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 5 
        AND DATEPART (DW, DateValue)=2
        GROUP BY YEAR (datevalue)
    )

-- independence day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Independence Day, '  WHERE (CalendarId % 2000) IN (704)

-- labor day (first Monday in September)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Labor Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT MIN (CalendarId)
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 9
        AND DATEPART (DW, DateValue)=2
        GROUP BY YEAR (datevalue)
    )

-- thanksgiving day (fourth Thursday in November)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Thanksgiving Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT Max (CalendarId)-2
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 11
        AND DATEPART (DW, DateValue)=7
        GROUP BY YEAR (datevalue)
    )

-- christmas
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Christmas Day, '  WHERE (CalendarId % 2000) IN (1225)

--=========================== Other named days
-- new years eve
UPDATE dbo.Calendar SET USADayName += 'New Year''s Eve, '  WHERE (CalendarId % 2000) IN (1231)

-- black friday (day after thanksgiving day)
UPDATE dbo.Calendar SET USADayName += 'Black Friday, '  WHERE CalendarId IN (SELECT CalendarId+1 From dbo.Calendar Where USADayName like '%Thanksgiving%')

-- christmas eve
UPDATE dbo.Calendar SET USADayName += 'Christmas Eve, '  WHERE (CalendarId % 2000) IN (1224)

-- boxing day
UPDATE dbo.Calendar SET USADayName += 'Boxing Day, '  WHERE (CalendarId % 2000) IN (1226)

--=========================== Remove trailing comma
UPDATE dbo.Calendar SET USADayName = SubString (USADayName, 1, LEN (USADayName) -1) WHERE LEN (USADayName) > 2

SELECT * FROM dbo.Calendar




这是此命令的输出



我已经看到数据架构师以各种风格实现了类似的结构。

我的问题是:我可以将哪些其他数据仓库/维度样式有用信息添加到此表结构中?

For my data warehouse, I am creating a calendar table as follows:

SET NOCOUNT ON

DROP Table dbo.Calendar
GO
Create Table dbo.Calendar
(
    CalendarId              Integer NOT NULL,
    DateValue               Date    NOT NULL,
    DayNumberOfWeek         Integer NOT NULL,
    NameOfDay               VarChar (10) NOT NULL,
    NameOfMonth             VarChar (10) NOT NULL,
    WeekOfYear              Integer NOT NULL,
    JulianDay               Integer NOT NULL,
    USAIsBankHoliday        Bit     NOT NULL,
    USADayName              VarChar (100) NULL,
)
ALTER TABLE dbo.Calendar ADD CONSTRAINT
    DF_Calendar_USAIsBankHoliday DEFAULT 0 FOR USAIsBankHoliday
GO
ALTER TABLE dbo.Calendar ADD CONSTRAINT
    DF_Calendar_USADayName DEFAULT '' FOR USADayName
GO

Declare @StartDate  DateTime = '01/01/2000'
Declare @EndDate    DateTime = '01/01/2020'

While @StartDate < @EndDate
Begin
    INSERT INTO dbo.Calendar 
    (
        CalendarId, 
        DateValue, 
        WeekOfYear,
        DayNumberOfWeek,
        NameOfDay,
        NameOfMonth,
        JulianDay
    )
    Values 
    (
        YEAR (@StartDate) * 10000 + MONTH (@StartDate) * 100 + Day (@StartDate), --CalendarId
        @StartDate,                 -- DateValue
        DATEPART (ww, @StartDate),  -- WeekOfYear
        DATEPART (dw, @StartDate),  -- DayNumberOfWeek
        DATENAME (dw, @StartDate),  -- NameOfDay
        DATENAME (M, @StartDate),   -- NameOfMonth
        DATEPART (dy, @StartDate)   -- JulianDay
    )

    Set @StartDate += 1
End

--=========================== Weekends
-- saturday and sunday
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Weekend, ' WHERE DayNumberOfWeek IN (1, 7) 


--=========================== Bank Holidays
-- new years day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'New Year''s Day, '  WHERE (CalendarId % 2000) IN (101)

-- memorial day (last Monday in May)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Memorial Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT MAX (CalendarId)
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 5 
        AND DATEPART (DW, DateValue)=2
        GROUP BY YEAR (datevalue)
    )

-- independence day
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Independence Day, '  WHERE (CalendarId % 2000) IN (704)

-- labor day (first Monday in September)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Labor Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT MIN (CalendarId)
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 9
        AND DATEPART (DW, DateValue)=2
        GROUP BY YEAR (datevalue)
    )

-- thanksgiving day (fourth Thursday in November)
UPDATE dbo.Calendar 
SET USAIsBankHoliday = 1, 
    USADayName += 'Thanksgiving Day, '  
WHERE 1=1
AND CalendarId IN 
    (
        SELECT Max (CalendarId)-2
        FROM dbo.Calendar 
        WHERE MONTH (DateValue) = 11
        AND DATEPART (DW, DateValue)=7
        GROUP BY YEAR (datevalue)
    )

-- christmas
UPDATE dbo.Calendar SET USAIsBankHoliday = 1, USADayName += 'Christmas Day, '  WHERE (CalendarId % 2000) IN (1225)

--=========================== Other named days
-- new years eve
UPDATE dbo.Calendar SET USADayName += 'New Year''s Eve, '  WHERE (CalendarId % 2000) IN (1231)

-- black friday (day after thanksgiving day)
UPDATE dbo.Calendar SET USADayName += 'Black Friday, '  WHERE CalendarId IN (SELECT CalendarId+1 From dbo.Calendar Where USADayName like '%Thanksgiving%')

-- christmas eve
UPDATE dbo.Calendar SET USADayName += 'Christmas Eve, '  WHERE (CalendarId % 2000) IN (1224)

-- boxing day
UPDATE dbo.Calendar SET USADayName += 'Boxing Day, '  WHERE (CalendarId % 2000) IN (1226)

--=========================== Remove trailing comma
UPDATE dbo.Calendar SET USADayName = SubString (USADayName, 1, LEN (USADayName) -1) WHERE LEN (USADayName) > 2

SELECT * FROM dbo.Calendar

Here is the output of this command

I have seen similar structures implemented in various flavours by data architects.

My question is: What other data warehousing / dimension style useful information can I add to this table structure?

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

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

发布评论

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

评论(4

山川志 2024-08-05 08:44:24
  • 季度
  • 财务/会计年
  • 财务/会计季度
  • isWeekend
  • isWeekday
  • isWorkDay
  • WeekId(自年初以来的周数)
  • isLastDayofMonth
  • DaysSince(例如自 1/1/2000 以来的天数)
  • Quarter
  • Year
  • Financial/Accounting Year
  • Financial/Accounting Quarter
  • isWeekend
  • isWeekday
  • isWorkDay
  • WeekId (weeks since start of year)
  • isLastDayofMonth
  • DaysSince (e.g. days since 1/1/2000)
雪化雨蝶 2024-08-05 08:44:24

这是我在日历维度中可能列的列表:

  • 关键
  • 日期
  • 是昨天
  • 是今天
  • 是明天
  • 一年中的
  • 一天 半年中的
  • 一天 季度中的
  • 一天 月份
  • 中的一天 星期几 星期
  • 几 短名称
  • 星期几 短名称 CS
  • 星期几 长名称
  • 星期几 长名称 CS 星期
  • 中的天数 月份
  • 中的天数
  • 季度中的天
  • 数 半年中的
  • 天数
  • 反转星期几
  • 反转月份中的天
  • 反转季度中的天
  • 反转半年中的天
  • 反转一年中的天
  • 过去 7 天
  • 是 过去 14 天
  • 是过去 30 天
  • 是过去 90 天
  • 是过去 180 天
  • 是最后 365 天
  • 是工作日
  • 是周末
  • 周中工作日
  • 工作日 季度
  • 月中
  • 工作日半年
  • 工作日 年度
  • 反向工作日 周中
  • 反向工作日 月中
  • 反向工作日 季度反向工作日
  • 半年中反向工作日
  • 反转年度工作日
  • 周工作日
  • 月工作日 季度
  • 工作日 半年
  • 工作日
  • 年度
  • 工作日 周中最后一个工作日 月中
  • 最后一个工作日
  • 工作日
  • 假期
  • 未来
  • 过去
  • 上个月 当前
  • 月份
  • 下个月
  • 本月至今
  • 是 月初 是 月末
  • 上个月
  • 月初 月末 月
  • 月份
  • 名称 长
  • 月份名称 长 CS
  • 月份名称 短
  • 月份名称 短 CS
  • 季度月份
  • 半年月份 上
  • 一周
  • 是当前周
  • 是下一周
  • 是周迄今为止
  • 周初 周末
  • 过去一周 周初
  • 周末
  • 周名称 长
  • 周名称 短
  • 周 本月的周
  • 一季度是当前季度
  • 是下季度 是季度
  • 初 是 季度
  • 是 季度末季度
  • 已过去 季度
  • 季度初 季度
  • 季度编号
  • 季度名称 长
  • 季度名称 长 CS
  • 季度名称 短
  • 前半年
  • 当前
  • 半年 下半年
  • 半年
  • 至今 半年初 半年
  • 已过
  • 半年 半年末
  • 年初半年数
  • 半年数
  • 半年名称长
  • 半年名称长 CS
  • 半年名称短 上
  • 一年
  • 是本年
  • 一年
  • 是今年 至今
  • 是年初是 年末
  • 是上年
  • 年初
  • 是年末 年号
  • 名称
  • 长年
  • 年份 名称 短
  • 年季度文本
  • 年月日
  • 年半年
  • 年季度
  • 年月 年
  • 闰年
  • 距离今天的天数
  • 距离 今天的工作日
  • 距离 距离今天的日历周数
  • 距离今天的日历月距离
  • 距离今天的日历季度距离
  • 距今天的日历半年距离
  • 距今天的日历年距离
  • 每月第 N 周的第 N 天
  • 反转该月第 N 周的第 N 天

我创建了交互式 电子表格,其中您可以为 PostgreSQL 数据库创建自己的时间维度。

This is my list of possible columns in calendar dimension:

  • Key
  • Date
  • Is Yesterday
  • Is Today
  • Is Tomorrow
  • Day of Year
  • Day of Halfyear
  • Day of Quarter
  • Day of Month
  • Day of Week
  • Day of Week Short Name
  • Day of Week Short Name CS
  • Day of Week Long Name
  • Day of Week Long Name CS
  • Days in Week
  • Days in Month
  • Days in Quarter
  • Days in Halfyear
  • Days in Year
  • Reverse Day of Week
  • Reversse Day of Month
  • Reverse Day of Quarter
  • Reverse Day of Halfyear
  • Reverse Day of Year
  • Is Last 7 days
  • Is Last 14 days
  • Is Last 30 days
  • Is Last 90 Days
  • Is Last 180 Days
  • Is Last 365 Days
  • Is Weekday
  • Is Weekend
  • Workday of Week
  • Workday of Month
  • Workday of Quarter
  • Workday of Halfyear
  • Workday of Year
  • Reverse Workday of Week
  • Reverse Workday of Month
  • Reverse Workday of Quarter
  • Reverse Workday of Halfyear
  • Reverse Workday of Year
  • Workdays in Week
  • Workdays in Month
  • Workdays in Quarter
  • Workdays in Halfyear
  • Workdays in Year
  • Is Last Workday in Week
  • Is Last Workday in Month
  • Is Workday
  • Is Holiday
  • Is Future
  • Is Past
  • Is Previous Month
  • Is Current Month
  • Is Following Month
  • Is Month to Date
  • Is Beginning of Month
  • Is End of Month
  • Is Past Month
  • Beginning of Month
  • End of Month
  • Month Number
  • Month Name Long
  • Month Name Long CS
  • Month Name Short
  • Month Name Short CS
  • Month of Quarter
  • Month of Halfyear
  • Is Previous Week
  • Is Current Week
  • Is Following Week
  • Is Week to Date
  • Is Beginning of Week
  • Is End of Week
  • Is Past Week
  • Beginning of Week
  • End of Week
  • Week Number
  • Week Name Long
  • Week Name Short
  • Week of Month
  • Is Previous Quarter
  • Is Current Quarter
  • Is Following Quarter
  • Is Quarter to Date
  • Is Beginning of Quarter
  • Is End of Quarter
  • Is Past Quarter
  • Beginning of Quarter
  • End of Quarter
  • Quarter Number
  • Quarter Name Long
  • Quarter Name Long CS
  • Quarter Name Short
  • Is Previous Halfyear
  • Is Current Halfyear
  • Is Following Halfyear
  • Is Halfyear to Date
  • Is Beginning of Halfyear
  • Is End of Halfyear
  • Is Past Halfyear
  • Beginning of Halfyear
  • End of Halfyear
  • Halfyear Number
  • Halfyear Name Long
  • Halfyear Name Long CS
  • Halfyear Name Short
  • Is Previous Year
  • Is Current Year
  • Is Following Year
  • Is Year to Date
  • Is Beginning of Year
  • Is End of Year
  • Is Past Year
  • Beginning of Year
  • End of Year
  • Year Number
  • Year Name Long
  • Year Name Short
  • Year Quarter Text
  • Year Month Day
  • Year Halfyear
  • Year Quarter
  • Year Month
  • Year Day of Year
  • Is Leap Year
  • Distance in Days from Today
  • Distance in Working Days from Today
  • Distance in Calendar Weeks from Today
  • Distance in Calendar Months from Today
  • Distance in Calendar Quarters from Today
  • Distance in Calendar Halfyears from Today
  • Distance in Calendar Years from Today
  • Nth Day of Week in Month
  • Reverse Nth Day of Week in Month

I created interactive spreadsheet where you can create your own time dimension for PostgreSQL database.

北音执念 2024-08-05 08:44:24

Raj More,这是一篇不错的帖子,对于创建日历非常有用的脚本,
您可以包含在同一个表中的其他字段可能是 -
1) 年度第四季度
2) 是周末
3) 是工作日

Well Raj More, Its a nice Post and very helpful script for Creating a calender,
The Other fields which you can include in the same table may be-
1) QuateroftheYear
2) IsWeekend
3) IsWeekday

深海不蓝 2024-08-05 08:44:24

一年中的季度
年_季度 (2013-3)
一年中的月份
年_月 (2013-08)
一年中的第几周
每月第几周
一年中的某一天
每季度的某一天
一个月中的哪一天
星期几

quarter of year
year_quarter (2013-3)
month of year
year_month (2013-08)
week of year
week of month
day of year
day of quarter
day of month
day of week

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