已将以下代码添加到我的 SQL 查询中:(注意缩减版)
DECLARE @rowType AS TABLE (
rowTypeLabel NVARCHAR (20));
INSERT INTO @rowType
VALUES ('Cumulative');
INSERT INTO @rowType
VALUES ('Non-Cumulative');
--select * from @rowType
SELECT ID,Name,StartDate,
EndDate,
rowTypeLabel AS Period
FROM dbo.sicky CROSS JOIN @rowType
WHERE (rowTypeLabel = 'Cumulative'
OR (rowTypeLabel = 'Non-Cumulative'
AND (EndDate IS NULL
OR EndDate BETWEEN CAST (DateAdd(Day, 1 - Day(getDate()), getdate()) AS DATE) AND CAST (DateAdd(month, 1, DateAdd(Day, -Day(getDate()), getdate())) AS DATE))));
运行时间从大约 10 分钟到大约 1 小时,有人对为什么会这样有任何建议吗?没有这个交叉连接的结果大约是 46,000,之后又带回了额外的 231 行(任何被归类为“非-cumulative' 根据查询。