在上图中,您将注意到前6行显示每个StudentID的2条记录。我需要为每组StudentID更新第二条记录的EndDate列值,其值为ONE DAY,而不是同一StudentID的第一条记录的StatusEffectiveDate。我正在使用SQL Server 2014.
所以第一条记录应该是这样的:
这是我的SQL代码:
py -3.5 -m pip install git+https://github.com/abenassi/Google-Search-API/
任何帮助/指示将不胜感激。
答案 0 :(得分:1)
使用ZLK所述的CTE来更新行。
;WITH cte AS
(SELECT
*
, DATEADD(DAY, -1, LAG(statuseffectivedate) OVER (PARTITION BY StudentID ORDER BY statuseffectivedate DESC)) AS NewDate
FROM #TEMP_99RecordsNeedingEndDateUpdated)
UPDATE cte
SET cte.enddate = cte.NewDate
WHERE cte.NewDate IS NOT NULL
答案 1 :(得分:1)
参加晚会,但这里还有一个尝试:
CALCULATE(SUM(TABLE1[FIELD_MEASURE]),DATEADD(TABLE1[DAY_ID].[Date],-1,YEAR))
正如其他人所说,这对您的数据的限制非常有限。
基本上,我是根据来自同一学生ID的MAX()日期的子查询,将EndDate更新为新值。最后,为了避免更新所有学生记录,我确保StatusEffectiveDate不是更晚的日期。
答案 2 :(得分:0)
with cte as
( select *
, row_number over (partition by studentID order by StatusEffectiveDate desc ) as rnD
, row_number over (partition by studentID order by StatusEffectiveDate asc ) as rnA
from table t
)
select cte.StudentID, cte.Location, cte.Status, cte.EconDisCode, cte.StatusEffectiveDate
, cte.EndDate
from cte
where rnD == 1
union
select cteA.StudentID, cteA.Location, cteA.Status, cteA.EconDisCode, cteA.StatusEffectiveDate
, dateadd(d, -1, cteD.StatusEffectiveDate)
from cte cteA
join cte cteD
on cteA.StudentID = cteD.StudentID
and cteA.rnD = 1
and cteD.rnA = 1
order by studentID, StatusEffectiveDate desc
update cteA
set cteA.EndDate = dateadd(d, -1, cteD.StatusEffectiveDate)
from cte cteA
join cte cteD
on cteA.StudentID = cteD.StudentID
and cteA.rnD = 1
and cteD.rnA = 1
order by studentID, StatusEffectiveDate desc
包含实际文字(不是图片),因此可以剪切和粘贴
答案 3 :(得分:0)
这个限制非常有限,特别是总是需要两行 - 一个' Free'和一个其他的' - 但如果是这种情况,您应该能够进行UNION查询。
SELECT StudentID, Location, Status, EconDisCode, StatusEffDate, EndDate
FROM #TEMP_99RecordsNeedingEndDateUpdated
WHERE Status = 'Free'
UNION
SELECT StudentID, Location, Status, EconDisCode, StatusEffDate, (SELECT StatusEffDate-1 FROM #TEMP_99RecordsNeedingEndDateUpdated t2 WHERE Status = 'Free' AND t1.StudentID = t2.StudentID) AS EndDate
FROM #TEMP_99RecordsNeedingEndDateUpdated t1
WHERE Status = 'Other'
我认为数据的小提琴示例会有所帮助。