以下脚本计算扣除其他特定时间间隔后的剩余时间间隔。
最后,我留下了充满空隙的区间。
这是我创建的脚本。有谁知道更好的方法吗?
SELECT * FROM SuspensionPeriod susPer WHERE id_document = 564148
DECLARE @idDocument BIGINT, @docBeginDate DATETIME, @docEndDate DATETIME
SET @idDocument = 564148
SELECT @docBeginDate = start_date, @docEndDate = end_date FROM Document md WHERE md.id_document = @idDocument
SELECT RowNum = Row_number() OVER(ORDER BY suspended_from), *
INTO #SuspensionPeriods
FROM SuspensionPeriod susPer WHERE susPer.id_document = @idDocument
DECLARE @MaxRownum INT
SET @MaxRownum = (SELECT Max(RowNum) FROM #SuspensionPeriods)
DECLARE @Iter INT
SET @Iter = (SELECT Min(RowNum) FROM #SuspensionPeriods)
DECLARE @intervalBegin DATETIME, @intervalEnd DATETIME
WHILE @Iter <= @MaxRownum
BEGIN
IF @Iter = 1
BEGIN
SET @intervalBegin = @docBeginDate
SELECT @intervalEnd = suspended_from FROM #SuspensionPeriods WHERE RowNum = @Iter
print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
print '============================='
--SELECT @intervalBegin = suspended_until FROM #SuspensionPeriods WHERE RowNum = @Iter
--SELECT @intervalEnd = suspended_from FROM #SuspensionPeriods WHERE RowNum = @Iter + 1
--print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
--print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
--print '============================='
END
IF @Iter <> 1 AND @Iter <> @MaxRownum
BEGIN
SELECT @intervalBegin = suspended_until FROM #SuspensionPeriods WHERE RowNum = @Iter - 1
SELECT @intervalEnd = suspended_from FROM #SuspensionPeriods WHERE RowNum = @Iter
print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
print '============================='
SELECT @intervalBegin = suspended_until FROM #SuspensionPeriods WHERE RowNum = @Iter
SELECT @intervalEnd = suspended_from FROM #SuspensionPeriods WHERE RowNum = @Iter + 1
print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
print '============================='
END
IF @Iter = @MaxRownum
BEGIN
--SELECT @intervalBegin = suspended_until FROM #SuspensionPeriods WHERE RowNum = @Iter - 1
--SELECT @intervalEnd = suspended_from FROM #SuspensionPeriods WHERE RowNum = @Iter
--print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
--print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
--print '============================='
SELECT @intervalBegin = suspended_until FROM #SuspensionPeriods WHERE RowNum = @Iter
SELECT @intervalEnd = @docEndDate
print dbo.fnFormatDate(@intervalBegin,'yyyy.MM.dd')
print dbo.fnFormatDate(@intervalEnd,'yyyy.MM.dd')
print '============================='
END
-- run your operation here
SET @Iter = @Iter + 1
END
DROP TABLE #SuspensionPeriods
答案 0 :(得分:0)
怎么样
SELECT MIN(md.start_date), MIN(sp.suspended_from)
FROM Document md
JOIN SuspensionPeriod sp ON sp.id_document = md.id_document
WHERE md.id_document = @idDocument
UNION
SELECT sp.suspended_to, (SELECT COALESCE(MIN(spnext.suspended_from), md.end_date) FROM SuspensionPeriod spnext WHERE sp.id_document=spnext.id_document AND spnext.suspended_from > sp.suspended_to)
FROM Document md
JOIN SuspensionPeriod sp ON sp.id_document = md.id_document
WHERE md.id_document = @idDocument