我一直试图解决这个问题,但尚未找到解决方案。我正在处理一个SQL视图,其结果基于某些条件。我的SQL视图如下所示
ALTER VIEW [dbo].[vAeoiCaseClose]
AS
SELECT CaseReference, s.AccessNumber
FROM dbo.AeoiSdtTemp s
JOIN AeoiCaseManagement c on c.AccessNumber = s.AccessNumber
JOIN AeoiCaptureLog a on a.AccessNumber = c.AccessNumber
WHERE AscertainMethodId IS NOT NULL
AND c.StatusCode = 15 AND a.StatusCode IN (6, 13, 15)
AND DATEDIFF(dd, s.LastModifiedDate, GETDATE()) <= 80 AND DATEDIFF(dd, a.LastModifiedDate, GETDATE()) <= 80
AND (
(AscertainMethodId = 1 AND ExtendedStatusId = 1)
OR (AscertainMethodId = 2 AND ExtendedStatusId = 1)
OR (AscertainMethodId = 2 AND ExtendedStatusId = 4)
OR (AscertainMethodId = 3 AND ExtendedStatusId = 1)
OR (AscertainMethodId = 3 AND ExtendedStatusId = 4)
OR (AscertainMethodId = 4 AND ExtendedStatusId = 1)
OR (AscertainMethodId = 5 AND ExtendedStatusId = 1)
OR (AscertainMethodId = 5 AND ExtendedStatusId = 4)
)
GO
我必须添加另一个条件,因此这些记录也可以作为视图的结果。
条件:
在某些情况下, dbo.AeoiSdtTemp 中可能有2条相同(AccessNumber)
但(AscertainMethodId)
不同的记录,在这种情况下,该记录将无法使用 AeoiCaptureLog 表,但我需要这些作为视图结果的一部分。
请建议。感谢帮助。
示例:
dbo.AeoiSdtTemp
(AccessNumber) - 1111(AscertainMethodId) - 3 StatusCode = 15
(AccessNumber) - 1111(AscertainMethodId) - 5 StatusCode = 11
dbo.AeoiCaptureLog
dbo.AeoiCaseManagement
答案 0 :(得分:0)
没有测试的快速而肮脏的起点:使用第二个别名(s1和s2而不是s,或s和s1)使用s.AccessNumber将dbo.AeoiSdtTemp表连接到自身,如下所示:
...
FROM dbo.AeoiSdtTemp s1
...
JOIN dbo.AeoiSdtTemp s2 ON (
s2.AccessNumber = s1.AccessNumber
AND s2. AscertainMethodId != s1. AscertainMethodId
)
...
另外,请检查LEFT与RIGHT以及INNER与OUTER联接,这将帮助您确定上述条款对结果数据的影响方式。