2013-09-29 2 views
0

, 나는 달의 시작부터 오늘까지 일을 작업의 총 수를 표시해야 WorkingDaysToDate 속성을 계산하려면 날짜 차원의 작업 일을 계산하는 방법 9월 27일입니다날짜 차원에서

오늘, 예를 들어 말 20 일 :

9월 27일 (금) : (금요일), 다음

WorkingDaysToDate 한 달에 오늘까지 날짜를 작업의 총 수는 어느 주말 및 공휴일

그리고 결과가 있어야한다 빼기입니다 012 3,516,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 

그리고

는,이 날은 현재 날짜에 대한 결과를 줄 것이다, 그러나 나는 모든 날짜를 계산하려는 저장 in Date dimensions

누군가가 도와 줄 수 있습니까?

이 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) 

답변

2

OVER() clause이있는 창 함수는 요구 사항을 신속하게 처리 할 수 ​​있습니다. 이 기능은 SQL 2012 이상에서만 사용할 수 있습니다. ROWS UNBOUNDED PRECEDING은 2012 년 새로 도입되었습니다.

이 키는 PARTITION BY 절과 함께 ROWS UNBOUNDED PRECEDING입니다. 이 말은 일년에 한 번씩, 그 다음 해에 한 달에 한 번씩 조합하여 사례 진술 기준에 따라 작업 일수를 합산하는 것입니다.

SQL 서버 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 Fiddle for 2012 Version.

커서가 Calculate a Running Total in SqlServer 참조 SQL 서버 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 

SQL Fiddle for 2008 Version

2008 년에이 작업을 수행하는 가장 빠른 방법입니다. 그러나, 귀하의 요구 사항이 잘 작동합니다.

+0

대단히 감사합니다. 그러나 우리는 여전히 SQL 2008을 사용하고 있습니다. SQL 2008 버전 – user2827587

+0

에 대한 가능한 해결책은 무엇입니까? 오신 것을 환영합니다. 2008 버전을 포함 할 대답을 편집했습니다. – bdeem

+0

정말 고마워, 정말 고마워. :) – user2827587