下面 UPDATE A iam 更新 facot Sqares 表 -iam 使用 sqare 表中的 updatd 因子来计算参数中的最终值
DECLARE @Year char(8) = '2017';
WITH E1 AS(
SELECT N FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) E(N)
),
E3 AS(
SELECT a.N FROM E1 a, E1 b, E1 c
),
cteTally AS(
SELECT TOP (360) DATEADD( dd, (ROW_NUMBER() OVER( ORDER BY (SELECT NULL)) - 1), CAST(@Year AS date)) myDate
FROM E3
),
weekDays AS(
SELECT myDate, ROW_NUMBER() OVER(PARTITION BY MONTH(myDate) ORDER BY myDate) rn
FROM cteTally
WHERE DATENAME( WEEKDAY, myDate) NOT IN ('Saturday', 'Sunday')
),
Final as(
SELECT
myDate StartDay,
datename(dw,myDate) Name,
DATEADD(dd,19,myDate) EndDay,
DATEADD(MM, DATEDIFF(MM, 0, myDate), 0) FirstDay
FROM weekDays
WHERE rn = 1
)
SELECT FirstDay,StartDay,Name as StartName,
case when Name='Monday' THEN DATEADD(dd,18,StartDay)
when Name='Tuesday' THEN DATEADD(dd,17,StartDay)
when Name='Wednesday' THEN DATEADD(dd,16,StartDay)
when Name='Thursday' THEN DATEADD(dd,15,StartDay)
when Name='Friday' THEN DATEADD(dd,14,StartDay)
ELSE NULL END AS EndDate,
case when Name='Monday' THEN datename(dw,DATEADD(dd,18,StartDay))
when Name='Tuesday' THEN datename(dw,DATEADD(dd,17,StartDay))
when Name='Wednesday' THEN datename(dw,DATEADD(dd,16,StartDay))
when Name='Thursday' THEN datename(dw,DATEADD(dd,15,StartDay))
when Name='Friday' THEN datename(dw,DATEADD(dd,14,StartDay))
ELSE NULL END AS EndName
into #Dates FROM Final