简体   繁体   English

SQL条件案例

[英]SQL Conditional Case

I have a table that lists items and a status about these items. 我有一个表格列出项目和这些项目的状态。 The problem is that some items have multiple different status entries. 问题是某些项目有多个不同的状态条目。 For example. 例如。

HOST          Status
1.1.1.1       PASS
1.1.1.1       FAIL
1.2.2.2       FAIL
1.2.3.3       PASS
1.4.2.1       FAIL
1.4.2.1       FAIL
1.1.4.4       NULL

I need to return one status per asset. 我需要为每个资产返回一个状态。

 HOST Status 1.1.1.1 PASS 1.2.2.2 FAIL 1.2.3.3 PASS 1.4.2.1 FAIL 1.1.4.4 No Results 

I have been trying to do this with T-SQL Case statements but can't quite get it right. 我一直试图用T-SQL Case语句做到这一点,但不能完全正确。 The conditions are any Pass + anything is a Pass, Fail+ No Results is a fail and Null is No Results. 条件是任何Pass +任何东西都是Pass,Fail + No Results是失败,Null是No Results。

Try using a case statement to convert to ordered results and group on that, finally, you'll need to convert back to the nice, human-readable answer: 尝试使用case语句转换为有序结果并对其进行分组,最后,您需要转换回漂亮的,人类可读的答案:

with cte1 as (
  SELECT HOST,
         [statNum] = case
                      when Status like 'PASS' then 2
                      when Status like 'FAIL' then 1
                      else 0
                    end
  FROM table
)
SELECT HOST, case max(statNum) when 2 then 'PASS' when 1 then 'FAIL' else 'No Results' end
FROM cte1
GROUP BY HOST

NOTE: I used a CTE statement to hopefully make things a little clearer, but everything could be done in a single SELECT , like so: 注意:我使用CTE语句希望使事情更清晰,但所有事情都可以在一个SELECT ,如下所示:

SELECT HOST,
 [Status] = case max(case when Status like 'PASS' then 2 when Status like 'FAIL' then 1 else 0 end)
    when 2 then 'PASS'
    when 1 then 'FAIL'
    else 'No Result'
   end
FROM table

You can use Max(Status) with Group by Host to get Distinct values: 您可以将Max(Status)Group by Host以获得Distinct值:

Select host, coalesce(Max(status),'No results') status
From Table1
Group by host
Order by host

Fiddle Demo Results: 小提琴演示结果:

|    HOST |     STATUS |
|---------|------------|
| 1.1.1.1 |       PASS |
| 1.1.4.4 | No results |
| 1.2.2.2 |       FAIL |
| 1.2.3.3 |       PASS |
| 1.4.2.1 |       FAIL |

By default SQL Server is case insensitive, If case sensitivity is a concern for your server, then use the lower() function as below: 默认情况下,SQL Server不区分大小写,如果区分大小写是服务器的问题,则使用lower()函数,如下所示:

Select host, coalesce(Max(Lower(status)),'No results') status
From Table1
Group by host
Order by host

Fiddle demo 小提琴演示

WITH CTE( HOST, STATUSValue)
AS(
     SELECT HOST,
     CASE STATUS WHEN 'PASS' 1 ELSE 0 END AS StatusValue
     FROM Data
)

SELECT DISTINCT HOST,
  CASE ISNULL(GOOD.STATUSVALUE,-1) WHEN 1 THEN 'Pass' 
       ELSE CASE ISNULL( BAD.STATUSVALUE,-1) WHEN 0 Then 'Fail' Else 'No Results' END
  END AS Results
FROM DATA AS D
LEFT JOIN CTE AS GOOD
  ON GOOD.HOST = D.HOST
 AND GOOD.STATUSVALUE = 1
LEFT JOIN CTE AS BAD
  ON BAD.HOST = BAD.HOST
 AND BAD.STATUSVALUE = 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM