我正在尝试编写一个CASE语句,它将为每个资产提供一个独特的价值。下面的代码执行时没有错误,但如果资产通过和失败,则写入每个结果。我每个资产只需要一个不同的结果,它基于CASE声明。
以下是查询的当前表单
的示例结果ServerID IP HOSTNAME CARS_SubBusiness STATUS Technology FailureReason
20979 1.1.1.1 myhost1 Business1 PASSED Windows -
20979 1.1.1.1 myhost1 Business1 FAILED UNIX Unable to complete Unix login
在这种情况下,我只需要返回PASSED结果。
Exclusion + * = Exclusion
Pass + * = Pass
FAIL + Not Attempted/No Results = FAIL
Not Attempted = Not Attempted
No Results = No Results
如果它通过并且失败我只需要传递信息,如果它被排除但有任何其他状态我只需要排除作为状态等等。
SELECT auth.ServerID, auth.IP, auth.HOSTNAME, auth.CARS_SubBusiness,
CASE MAX (CASE
(CASE
(CASE WHEN [Status] LIKE 'PASSED' AND ExclusionType IS NOT NULL
THEN 6 WHEN [Status] LIKE 'FAILED' AND ExclusionType IS NOT NULL THEN 6 WHEN [STATUS] LIKE 'Not Attempted' AND ExclusionType IS NOT NULL
THEN 6 WHEN [Status] LIKE 'PASSED' AND ExclusionType IS NULL THEN 5 WHEN [Status] LIKE 'FAILED' AND ExclusionType IS NULL
THEN 4 WHEN [STATUS] LIKE 'Not Attempted' AND ExclusionType IS NULL THEN 3 ELSE 0 END)
WHEN 6 THEN 'Excluded' WHEN 5 THEN 'PASSED' WHEN 4 THEN 'FAILED' WHEN 3 THEN 'Not Attempted' ELSE 'No Results' END)
WHEN 'Excluded' THEN 5
WHEN 'Not Attempted' THEN 4
WHEN 'No Results' THEN 3
WHEN 'PASSED' then 2
WHEN 'FAILED' then 1 END)
WHEN 5 THEN 'EXCLUDED'
WHEN 4 THEN 'Not Attempted'
WHEN 3 THEN 'No Results'
WHEN 2 THEN 'PASSED'
WHEN 1 THEN 'FAILED'
END AS [STATUS], auth.Technology,
auth.FailureReason
FROM _CombinedAuthentication AS auth RIGHT OUTER JOIN
BusinessTranslations AS bt ON auth.BusinessID = bt.BusinessID
WHERE (auth.ActiveFlag IS NOT NULL)
GROUP BY auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason
答案 0 :(得分:0)
您可能需要考虑使用集合,如下所示:
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status
from bla where status='exclusion'
union
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status
from bla where status='pass' and not exists (select 1 from bla where status='exclusion' and...)
union
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status
from bla where status='fail' and not exists (select 1 from bla where status='exclusion' and ...)
and not exists (select 1 from bla where status='pass' and ...)
union
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status
from bla where status='Not Attempted' and not exists (select 1 from bla where status='exclusion' and ...)
and not exists (select 1 from bla where status='pass' and ...)
and not exists (select 1 from bla where status='fail' and ..)
union
select auth.ServerID, auth.IP,auth.CARS_SubBusiness, auth.HOSTNAME, auth.Technology, auth.FailureReason,status
from bla where status='No Result' and not exists (select 1 from bla where status='exclusion' and ...)
and not exists (select 1 from bla where status='pass' and ...)
and not exists (select 1 from bla where status='fail' and ..)
and not exists (select 1 from bla where status='Not Attempted' and ..)
这个解决方案也可以使用CTE进行改进