SQL查询条件选择基于较新的条目

时间:2011-02-24 23:11:17

标签: sql

我有一个查询,它会在测试数据库中搜索失败的测试,并在特定测试集失败时返回某个代码。我需要它做的是,如果在已经通过的数据库中没有保存该测试的较新版本,则仅返回该代码。这是一组查询的一部分,给定一个项目编号,查询该编号的每个子集(有5个测试子集),然后检查并给它4种不同状态中的一种。这是一段摘录:

SELECT
MAX(
CASE
WHEN PROJECT.PARENT_ID IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM TESTRESULTS WHERE TESTRESULTS.RESULTID = '962'))) 
AND PROJECT.PROJECTNAME LIKE '%EMC%' 
THEN '4'
ELSE.....

我需要检查同一个表,查找属于同一个项目的同名项目的新条目,这些条目已通过测试而不是失败,即RESULTID ='961',而是让它落到另一个案例,而不是指定一个'4'。测试日期存储在表格中。我自己玩了一段时间,从桌子上做了一些连接回到自身,但这个父母的父母的父级的层次结构使得很难说。此外,我没有设计此数据库,也无法以任何方式更改架构。

编辑: 这是表的相关结构,以及一些示例记录:

ID | PARENT_ID | TEST TYPE | RESULTID | TESTDATE
-------------------------------------------------
4  |     1     | ESD       |   962    | 01-01-11
5  |     2     | CI        |   961    | 01-02-11
6  |     3     | ESD       |   961    | 02-01-11
7  |     8     | RI        |   962    | 02-02-11

我需要一个将返回失败的测试的查询(resultid 962),除非有另一个具有相同名称的测试,并且上面的查询中描述了相同的parent_id关系,但是通过了测试(resultid 961)。所以,如果我在01-01-11运行查询,我会得到ID为4的行,但如果我在02-01-11运行它,它将不返回任何行,并在02-02-11它将返回ID为7的行。此外,还有resultids> 962用于异常等,并且重新数据< 962。 961尚未发生的测试。

再次编辑:对象表结构

ID | PARENT_ID | Description   |
--------------------------------
50 |     0     | Product Level | (Very Top)
51 |    50     | Project Level | (The PROJECT table describes this lvl)
52 |    51     | Work Request  |
1  |    52     | Work Order    |
2  |    52     | Work Order    |
3  |    52     | Work Order    |
8  |    52     | Work Order    |

因此,引用上表,TESTRESULT表中的每个测试都是一个worker的子项,而worker又是一个工作请求的子项,它是项目的子项,也就是产品的子项。我需要能够将每个单独的测试结果都绑定到项目级别,这就是我运行PROJECT表而不是工作请求表的原因。

1 个答案:

答案 0 :(得分:0)

我重写了查询(供参考,原始答案如下)。

WHEN PROJECT.PARENT_ID IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM TESTRESULTS t1 WHERE t1.RESULTID = '962'
and not exists 
(select * from object o2 
WHERE PROJECT.PARENT_ID = o2.id and o2.id IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM object WHERE object.id IN
(SELECT parent_id FROM TESTRESULTS t2 WHERE t2.RESULTID = '961'
and t1.testtype = t2.testtype and t2.testdate > t1.testdate)))))))
AND PROJECT.PROJECTNAME LIKE '%EMC%' 

达到该结果的测试场景:

create table #tmpObj (ident int primary key, parent_id int, description varchar(20));
insert into #tmpObj (ident, parent_id, description) values (50, 0, 'Product Level');
insert into #tmpObj (ident, parent_id, description) values (60, 50, 'Project X');
insert into #tmpObj (ident, parent_id, description) values (70, 60, 'Work request 1');
insert into #tmpObj (ident, parent_id, description) values (101, 70, 'Work order 1a');
insert into #tmpObj (ident, parent_id, description) values (102, 70, 'Work order 1b');
insert into #tmpObj (ident, parent_id, description) values (103, 70, 'Work order 1c');
insert into #tmpObj (ident, parent_id, description) values (71, 60, 'Work request 2');
insert into #tmpObj (ident, parent_id, description) values (111, 71, 'Work order 2a');
insert into #tmpObj (ident, parent_id, description) values (112, 71, 'Work order 2b');
insert into #tmpObj (ident, parent_id, description) values (113, 71, 'Work order 2c');
insert into #tmpObj (ident, parent_id, description) values (61, 50, 'Project Z');
insert into #tmpObj (ident, parent_id, description) values (80, 61, 'Work request 1');
insert into #tmpObj (ident, parent_id, description) values (201, 80, 'Work order 1a');
insert into #tmpObj (ident, parent_id, description) values (202, 80, 'Work order 1b');
insert into #tmpObj (ident, parent_id, description) values (203, 80, 'Work order 1c');
insert into #tmpObj (ident, parent_id, description) values (81, 61, 'Work request 2');
insert into #tmpObj (ident, parent_id, description) values (211, 81, 'Work order 2a');
insert into #tmpObj (ident, parent_id, description) values (212, 81, 'Work order 2b');
insert into #tmpObj (ident, parent_id, description) values (213, 81, 'Work order 2c');

create table #tmpTest (ident int primary key, parent_id int, name1 varchar(10), resultid int, testdate date);
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2001, 101, 'ESD', 962, '2011-01-01');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2002, 102, 'CI', 961, '2011-01-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2003, 103, 'RI', 961, '2011-01-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2004, 111, 'ESD', 961, '2011-02-01');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2005, 112, 'CI', 962, '2011-02-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2006, 113, 'RI', 961, '2011-02-03');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2007, 201, 'ESD', 961, '2011-01-01');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2008, 202, 'CI', 961, '2011-01-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (2009, 203, 'RI', 961, '2011-01-02');

SELECT * FROM #tmpObj o1 WHERE o1.ident IN
(SELECT parent_id FROM #tmpObj WHERE #tmpObj.ident IN
(SELECT parent_id FROM #tmpObj WHERE #tmpObj.ident IN
(SELECT parent_id FROM #tmpTest t1 WHERE t1.RESULTID = 962
and not exists 
(select * from #tmpObj o2 
WHERE o1.ident = o2.ident and o2.ident IN
(SELECT parent_id FROM #tmpObj WHERE #tmpObj.ident IN
(SELECT parent_id FROM #tmpObj WHERE #tmpObj.ident IN
(SELECT parent_id FROM #tmpTest t2 WHERE t2.RESULTID = 961
and t1.name1 = t2.name1 and t2.testdate > t1.testdate))))))); 

原始答案:

嗯,给定的信息让我感到困惑,但似乎这需要一个EXISTS()子查询(我希望在interbase中可用),如下所示(简化,没有父母的东西)示例:

create table #tmpTest (ident int primary key, parent_id int, name1 varchar(10), resultid int, testdate date);
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (4, 1, 'ESD', 962, '2011-01-01');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (5, 2, 'CI', 961, '2011-01-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (6, 1, 'ESD', 961, '2011-02-01');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (7, 8, 'RI', 962, '2011-02-02');
insert into #tmpTest (ident, parent_id, name1, resultid, testdate) values (9, 8, 'RI', 962, '2011-02-03');

select * from #tmpTest t1 
where resultid = 962 and not exists 
(select ident from #tmpTest t2 
where t2.parent_id = t1.parent_id 
and t2.resultid = 961 
and t2.testdate > t1.testdate); 

我假设testresults表中的parent_id可用于标识测试类别,但这可能不准确。 我希望这至少可以帮助您优化查询。