在日期维度中,我想计算WorkingDaysToDate属性,该属性应指示从月初到今天的工作日总数,例如
今天是9月27日(星期五),然后是
WorkingDaysToDate是一个月到今天的工作日总数减去任何周末和公众假期
结果应该是:
9月27日(星期五):20个工作日, 9月28日(星期六):20, 9月29日(周日):20个工作日, 9月30日(星期一)那么应该有21个工作日 等等我得到了正确的结果,当天使用下面的代码,但我想计算日期维度中的所有日期:
WITH CTE AS
(
select * from dbo.Date_Dimension
where DATEPART(MONTH,GETDATE()) = CalendarMonthNumber
AND DATEPART(year,getdate()) = CalendarYearNumber
AND Fulldate between DATEADD(Month,DateDiff(Month,0,Getdate()),0)
AND convert (Date, GETDATE())
AND HolidayFlag = 0
AND WeekDayFlag = 1 )
Select COUNT (*) AS WorkingDayToDate from CTE
如上所述,这只会给我当前日期的结果,但我想计算和存储日期维度中的所有日期
有人可以帮忙吗
请参阅下面的创建表格代码和示例插入代码以填充日期维度以加快执行速度:
要创建Date_dimension表,请使用以下代码:
CREATE TABLE [dbo].[Date_Dimension](
[DateSK] [int] NOT NULL,
[FullDate] [datetime] NOT NULL,
[Day] [tinyint] NOT NULL,
[DaySuffix] [varchar](4) NOT NULL,
[DayOfWeek] [varchar](9) NOT NULL,
[DayOfWeekNumber] [int] NOT NULL,
[DayOfWeekInMonth] [tinyint] NOT NULL,
[DayOfYearNumber] [int] NOT NULL,
[RelativeDays] [int] NOT NULL,
[WeekOfYearNumber] [tinyint] NOT NULL,
[WeekOfMonthNumber] [tinyint] NOT NULL,
[RelativeWeeks] [int] NOT NULL,
[CalendarMonthNumber] [tinyint] NOT NULL,
[CalendarMonthName] [varchar](9) NOT NULL,
[RelativeMonths] [int] NOT NULL,
[CalendarQuarterNumber] [tinyint] NOT NULL,
[CalendarQuarterName] [varchar](6) NOT NULL,
[RelativeQuarters] [int] NOT NULL,
[CalendarYearNumber] [int] NOT NULL,
[RelativeYears] [int] NOT NULL,
[StandardDate] [varchar](10) NULL,
[WeekDayFlag] [bit] NOT NULL,
[HolidayFlag] [bit] NOT NULL,
[OpenFlag] [bit] NOT NULL,
[FirstDayOfCalendarMonthFlag] [bit] NOT NULL,
[LastDayOfCalendarMonthFlag] [bit] NOT NULL,
[HolidayText] [varchar](50) NULL,
CONSTRAINT [PK_DimDate] PRIMARY KEY CLUSTERED
(
[DateSK] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
GO
要填充样本数据,您可以使用以下代码:
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130901,'2013-09-01 00:00:00.000',1,'1st','Sunday',1,1,244,-26,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/01/2013',0,0,0,1,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130902,'2013-09-02 00:00:00.000',2,'2nd','Monday',2,1,245,-25,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/02/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130903,'2013-09-03 00:00:00.000',3,'3rd','Tuesday',3,1,246,-24,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/03/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130904,'2013-09-04 00:00:00.000',4,'4th','Wednesday',4,1,247,-23,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/04/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130905,'2013-09-05 00:00:00.000',5,'5th','Thursday',5,1,248,-22,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/05/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130906,'2013-09-06 00:00:00.000',6,'6th','Friday',6,1,249,-21,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/06/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130907,'2013-09-07 00:00:00.000',7,'7th','Saturday',7,1,250,-20,36,1,-3,9,'September',0,3,'Third',0,2013,0,'09/07/2013',0,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130908,'2013-09-08 00:00:00.000',8,'8th','Sunday',1,2,251,-19,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/08/2013',0,0,0,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130909,'2013-09-09 00:00:00.000',9,'9th','Monday',2,2,252,-18,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/09/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130910,'2013-09-10 00:00:00.000',10,'10th','Tuesday',3,2,253,-17,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/10/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130911,'2013-09-11 00:00:00.000',11,'11th','Wednesday',4,2,254,-16,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/11/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130912,'2013-09-12 00:00:00.000',12,'12th','Thursday',5,2,255,-15,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/12/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130913,'2013-09-13 00:00:00.000',13,'13th','Friday',6,2,256,-14,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/13/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130914,'2013-09-14 00:00:00.000',14,'14th','Saturday',7,2,257,-13,37,2,-2,9,'September',0,3,'Third',0,2013,0,'09/14/2013',0,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130915,'2013-09-15 00:00:00.000',15,'15th','Sunday',1,3,258,-12,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/15/2013',0,0,0,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130916,'2013-09-16 00:00:00.000',16,'16th','Monday',2,3,259,-11,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/16/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130917,'2013-09-17 00:00:00.000',17,'17th','Tuesday',3,3,260,-10,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/17/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130918,'2013-09-18 00:00:00.000',18,'18th','Wednesday',4,3,261,-9,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/18/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130919,'2013-09-19 00:00:00.000',19,'19th','Thursday',5,3,262,-8,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/19/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130920,'2013-09-20 00:00:00.000',20,'20th','Friday',6,3,263,-7,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/20/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130921,'2013-09-21 00:00:00.000',21,'21st','Saturday',7,3,264,-6,38,3,-1,9,'September',0,3,'Third',0,2013,0,'09/21/2013',0,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130922,'2013-09-22 00:00:00.000',22,'22nd','Sunday',1,4,265,-5,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/22/2013',0,0,0,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130923,'2013-09-23 00:00:00.000',23,'23rd','Monday',2,4,266,-4,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/23/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130924,'2013-09-24 00:00:00.000',24,'24th','Tuesday',3,4,267,-3,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/24/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130925,'2013-09-25 00:00:00.000',25,'25th','Wednesday',4,4,268,-2,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/25/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130926,'2013-09-26 00:00:00.000',26,'26th','Thursday',5,4,269,-1,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/26/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130927,'2013-09-27 00:00:00.000',27,'27th','Friday',6,4,270,0,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/27/2013',1,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130928,'2013-09-28 00:00:00.000',28,'28th','Saturday',7,4,271,1,39,4,0,9,'September',0,3,'Third',0,2013,0,'09/28/2013',0,0,1,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130929,'2013-09-29 00:00:00.000',29,'29th','Sunday',1,5,272,2,40,5,1,9,'September',0,3,'Third',0,2013,0,'09/29/2013',0,0,0,0,0,NULL)
INSERT INTO Date_dimension(DateSK,FullDate,Day,DaySuffix,DayOfWeek,DayOfWeekNumber,DayOfWeekInMonth,DayOfYearNumber,RelativeDays,WeekOfYearNumber,WeekOfMonthNumber,RelativeWeeks,CalendarMonthNumber,CalendarMonthName,RelativeMonths,CalendarQuarterNumber,CalendarQuarterName,RelativeQuarters,CalendarYearNumber,RelativeYears,StandardDate,WeekDayFlag,HolidayFlag,OpenFlag,FirstDayOfCalendarMonthFlag,LastDayOfCalendarMonthFlag,HolidayText) VALUES (20130930,'2013-09-30 00:00:00.000',30,'30th','Monday',2,5,273,3,40,5,1,9,'September',0,3,'Third',0,2013,0,'09/30/2013',1,0,1,0,1,NULL)
答案 0 :(得分:2)
具有OVER() clause的窗口功能可以快速满足您的要求。请注意,这仅适用于SQL 2012及更高版本,ROWS UNBOUNDED PRECEDING是2012年的新功能。
这个关键是与PARTITION BY子句一起使用ROWS UNBOUNDED PRECEDING。这就是说,按年份和月份分组,然后根据我的案例陈述标准对该年月组合总结工作日。
SQL Server 2012优化
select
fulldate,
sum(case
when holidayFlag = 0 and weekdayFlag = 1 then 1
else 0
end) over(partition by year(fulldate), month(fulldate)
order by fulldate
ROWS UNBOUNDED PRECEDING)
from date_dimension
SQL Server 2008
-- This will be slower than 2012 version
select
d1.fulldate,
(select
sum(case
when d2.holidayFlag = 0 and d2.weekdayFlag = 1 then 1
else 0
end)
from date_dimension d2
where d1.fulldate >= d2.fulldate
and year(d1.fulldate) = year(d2.fulldate)
and month(d1.fulldate) = month(d2.fulldate)) x
from date_dimension d1
group by d1.fulldate
2008年最快的方法是使用Calculate a Running Total in SqlServer光标。但是,根据您的要求,这应该可以正常工作。