【问题标题】:TSQL Nested CASE with Condition带条件的 TSQL 嵌套 CASE
【发布时间】:2014-01-27 01:49:13
【问题描述】:

我正在尝试编写一个 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

【问题讨论】:

  • 你的问题是……?
  • @JayC 我只需要每个资产的不同结果,它基于 CASE 语句。排除 + * = 排除 通过 + * = 通过 失败 + 未尝试/无结果 = 失败 未尝试 = 未尝试 无结果 = 无结果
  • “我只需要每个资产有一个不同的结果” - 好的,所以如果有 多个 可能的结果,我们(以及最终的 SQL Server)打算使用什么规则选择一个结果?这些规则必须基于我们可用的数据(例如,sproog 值最大的结果,其中sproog 是您的一个表中的一列)
  • 让我检查一下 - 您是否尝试为不同的场景设置一组不同的列?这在一个查询中很难。或者你可以让一些列在不需要时保持为 NULL,或者说“n/a”吗?你能发布一个示例结果集来显示你要去哪里吗?
  • 我发布了一个示例输出。并非所有重复项都是通过/失败。我想我需要的是一个 CASE 语句,它只会给我匹配 CASE 的整个记录​​。

标签: sql-server tsql nested case distinct-values


【解决方案1】:

您可能需要考虑使用集合,如下所示:

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 来改进

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    相关资源
    最近更新 更多