根据结果​​sql中的条件比较值和返回值

时间:2014-10-31 08:18:03

标签: sql sqlite

我有两个表:tests和test_status

测试:

--------------------
|  id  |  name     |
--------------------
|  1   |  Test 1   |
|  2   |  Test 2   |
|  3   |  Test 3   |
|  4   |  Test 1   |
|  5   |  Test 2   |
--------------------

test_status:

------------------------------------------
|  id  |  test_id  |  run_id  |  status  |
------------------------------------------
|  1   |  1        |  1       |  PASS    |
|  2   |  2        |  1       |  PASS    |
|  3   |  3        |  1       |  FAIL    |
|  4   |  4        |  2       |  FAIL    |
|  5   |  5        |  2       |  PASS    |
------------------------------------------

我想检索第一次运行时传递的测试名称,并在第二次运行时失败。所以在这种情况下,它必须显示 Test1 。我的SQL查询知识非常新手,因此寻求您的帮助。

2 个答案:

答案 0 :(得分:0)

我相信,您应该更改tests,以便每次测试都有一个条目:

create table tests (
   id    integer     primary key,
   name  varchar(10)
);

然后你会这样填写

insert into tests values (1, 'Test 1');
insert into tests values (2, 'Test 2');
insert into tests values (3, 'Test 3');

表格test_status几乎保持不变,只有test_id根据"最小化&#34}进行更改。 test表:

create table test_status (
  id      integer primary key,
  test_id integer references tests,
  run_id  integer,
  status  varchar(4)
);

它就像这样填充:

insert into test_status values (1, 1, 1, 'PASS');
insert into test_status values (2, 2, 1, 'PASS');
insert into test_status values (3, 3, 1, 'FAIL');
insert into test_status values (4, 1, 2, 'FAIL');
insert into test_status values (5, 2, 2, 'PASS');

然后,通过以下查询,您可以找到所需的记录:

select
  test.name
from
  tests test                                             join
  test_status first_run  on test.id = first_run.test_id  join
  test_status second_run on test.id = second_run.test_id
where
  first_run.status  = 'PASS' and
  second_run.status = 'FAIL' and
  first_run.run_id  = 1      and
  second_run.run_id = 2;

编辑所以,从问题的评论中,我认为你实际上想要的是n th 测试通过的测试,但是(n + 1) ) th 失败。在这种情况下,您将查询更改为

select
  test.name
from
  tests test                                             join
  test_status first_run  on test.id = first_run.test_id  join
  test_status second_run on test.id = second_run.test_id
where
  first_run.status  = 'PASS' and
  second_run.status = 'FAIL' and
  first_run.run_id  = second_run.run_id - 1 -- Change HERE!
;

EDIT II 因此,您对供应商对ERD的看法感到困惑。在这种情况下,您需要调整一下:

create table tests (
   id    integer     primary key,
   name  varchar(10)
);


insert into tests values (1, 'Test 1');
insert into tests values (2, 'Test 2');
insert into tests values (3, 'Test 3');
insert into tests values (4, 'Test 1');
insert into tests values (5, 'Test 2');

create table test_status (
  id      integer primary key,
  test_id integer references tests,
  run_id  integer,
  status  varchar(4)
);

insert into test_status values (1, 1, 1, 'PASS');
insert into test_status values (2, 2, 1, 'PASS');
insert into test_status values (3, 3, 1, 'FAIL');
insert into test_status values (4, 4, 2, 'FAIL');
insert into test_status values (5, 5, 2, 'PASS');

查询变得有点笨拙

select
  test_1.name
from
  tests       test_1                                  join
  tests       test_2  on test_1.name = test_2.name    join
  test_status run_1   on test_1.id   = run_1.test_id join
  test_status run_2   on test_2.id   = run_2.test_id
where
  run_1.status = 'PASS' and
  run_2.status = 'FAIL' and
  run_1.run_id = run_2.run_id - 1;

sql fiddle

相同的查询

答案 1 :(得分:0)

select tests.name from tests 
inner join test_status maxRun on tests.id = maxRun.test_id
inner join test_status maxRun_1 on tests.id = maxRun_1.test_id
where maxRun.run_id = (select max(run_id) from test_status max where tests.id = max.test_id)
and maxRun_1.run_id = (select max(run_id) -1 from test_status max where tests.id = max.test_id)
and maxRun.status = 'FAIL'
and maxRun_1.status = 'PASS'

您可以尝试一下,不确定它是否有效,如果您可以在小提琴中创建测试数据,我可以帮助测试查询。