在添加到数据库中的最终表之前,我有一个数据保存表。
该表中有一个 datediff 计算,它返回一个月中的天数。
但是,这是基于项目的,我需要做的是在另一个表中查找项目 ID,以查看 TimeID(年/月)是否等于项目的开始日期或结束日期。
当它匹配时,它需要将该月的天数更改为 Month-Start 到 Project-Finish (如果它是结束月份)或 Project-Start 到 Month-End (如果它是开始月份)。
我尝试使用以下脚本执行此操作,但出现错误:
*** This bit is sorted, please see below ***
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'A'.
*** This bit is sorted, please see below ***
* 编辑 * 感谢 Anton,我已将所需的 END 添加到我的 CASE 语句中,但我现在得到一组不同的错误(可能取决于我的语法):
Msg 207, Level 16, State 1, Line 23
Invalid column name 'TimeID'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'TimeID'.
Msg 207, Level 16, State 1, Line 26
Invalid column name 'ID'.
Msg 207, Level 16, State 1, Line 28
Invalid column name 'ID'.
如果可以的话请帮忙。
脚本是:
UPDATE FAC
SET FAC.[SignedData] =
CASE
WHEN FAC.[TIMEID] = A.[Start_TimeID] THEN
CASE
WHEN pd.[Start_Date] >= GETDATE() THEN datediff(day,pd.[Start_Date],pt.[Period_End])
WHEN pd.[Start_Date] < GETDATE() THEN datediff(day,GETDATE(),pt.[Period_End])
END
WHEN FAC.[TIMEID] = A.[End_TimeID] THEN
CASE
WHEN pd.[End_Date] >= GETDATE() THEN datediff(day,getdate(),pd.[End_Date])
WHEN pd.[End_Date] < GETDATE() THEN '0'
END
END
FROM Temp_Fac2Programme FAC
JOIN
(
SELECT
pd.[TimeID], pd.[Start_TimeID], pd.[End_TimeID], pt.[Period_Start], pt.[Period_End]
FROM ProjectDates pd
JOIN ProjectTimeID pt
on pd.[TimeID] = pt.[TimeID]
) A
ON A.[ID] = FAC.[Project]
Where A.[ID] = FAC.[Project]
GO
NB - SIGNEDDATA 是该月的天数。
* 示例数据 *
Temp_Fac2Programme (FAC) 包含:
ACCOUNT CATEGORY DATASRC PROFITCENTRE PROJECT RPTCURRENCY TIMEID SIGNEDDATA SOURCE
REMAIN_DAYS_FLAG ACTUAL DS_FLAGS B9059 AAA_7915_BBOY LC 20130100 34.0000000000 0
ProjectDates (pd) 包含:
PROJECT_ID START_TIMEID END_TIMEID START_DATE END_DATE TOTAL_DAYS
PAG_5244_CASH 20110400 20120300 2011-04-01 2012-03-31 365
ProjectTimeID (pt) 包含:
TIMEID PERIOD_START PERIOD_END DAYS_IN_PERIOD PCMONTHSTAT
20140600 2014-05-31 2014-06-27 27 F