需要测试用例以查找名称的缺失值

时间:2019-10-21 14:58:20

标签: sql-server

下表有示例数据

EitherT

它具有以下数据:

enter image description here

我需要在Sql中使用一个测试用例来查找名称的缺失产品,并且每个名称中的产品在新产品之前都必须具有产品 例如, A在3处缺少产品,而在4处产品失败 B缺少产品直到3,但在4时丢失 C有产品的,合格 D在1处缺少产品,失败

您能帮我显示如下输出:

enter image description here

2 个答案:

答案 0 :(得分:2)

尝试一下:

select name, 
       min(case when val = '' and next_val <> '' 
            then 'FAILED' 
            else 'PASS' 
       end) 
from   ( select *, lead(val) over ( partition by name order by col ) next_val from @test
         unpivot
         ( val for col in (Product_1, Product_2, Product_3, Product_4) ) as unpvt 
       ) A
group  by name;

答案 1 :(得分:1)

您的查询应如下所示,但您需要仔细设置条件  对于失败和成功: SQL Fiddle

MS SQL Server 2017架构设置

create table test (
   Name varchar(30) not null,
   Product_1 varchar(10) not null,
   Product_2 varchar(10) not null,
   Product_3 varchar(10) not null,
   Product_4 varchar(10) not null
)

insert into test values('A', 'AB', 'BC', '', 'FG')
insert into test values('B', 'EF1', 'EF2','EF3','')
insert into test values('C', 'ZA1', 'ZA2', 'ZA3','ZA4')
insert into test values('D', '', 'ER','TZ1','GB1')

查询1

SELECT Name,
CASE
    WHEN Product_1 = '' THEN 'FAILED'
    WHEN Product_2 = '' THEN 'FAILED'
    WHEN Product_3 = '' THEN 'FAILED'
    WHEN Product_4 = '' THEN 'PASS'
    ELSE 'PASS'
END AS Testing
FROM test

Results

| Name | Testing |
|------|---------|
|    A |  FAILED |
|    B |    PASS |
|    C |    PASS |
|    D |  FAILED |