我有一张表格:
test_name | test_result
-----------------------
test1 | pass
test2 | fail
test1 | pass
test1 | pass
test2 | pass
test1 | pass
test3 | pass
test3 | fail
test3 | pass
正如你可以看到所有test1的传递,而test2和test3都传递并失败。
是否有一个SQL语句可用于返回仅通过的测试的不同名称?例如。 TEST1
答案 0 :(得分:5)
您可以按test_name进行分组,计算传递次数,并检查它是否与组中的行数相同。
SELECT test_name
FROM table1
GROUP BY test_name
HAVING SUM(test_result = 'pass') = COUNT(*)
或者,计算失败次数并检查它是否为零:
SELECT test_name
FROM table1
GROUP BY test_name
HAVING SUM(test_result = 'fail') = 0
结果:
test1
测试数据:
CREATE TABLE table1 (test_name NVARCHAR(100) NOT NULL, test_result NVARCHAR(100) NOT NULL);
INSERT INTO table1 (test_name, test_result) VALUES
('test1', 'pass'),
('test2', 'fail'),
('test1', 'pass'),
('test1', 'pass'),
('test2', 'pass'),
('test1', 'pass'),
('test3', 'pass'),
('test3', 'fail'),
('test3', 'pass');
答案 1 :(得分:0)
这可能不是最简单的方法(我倾向于滥用子查询),但你可以这样做:
SELECT test_name FROM tests AS a WHERE NOT EXISTS (SELECT test_name FROM tests AS b WHERE a.test_name = b.test_name AND test_result != 'pass') GROUP BY test_name;