我重寫了查詢(供參考,原始答案如下)。
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可以用來標識測試類別,但這可能不準確。 我希望這至少可以幫助您優化您的查詢。
數據結構和樣本記錄? – RichardTheKiwi 2011-02-24 23:16:02
哪個數據庫。如果它的oracle,那麼有一個簡單的方法來處理像這樣使用Connect By Prior的父層次結構。 – 2011-02-24 23:16:35
它使用interbase,並得到一個準確的數據結構的想法,ID必須顯示幾個樣本記錄,我沒有時間去匿名足夠的他們,但基本的想法是這樣的: 對象表包含每個對象(項目,測試集,測試,工作順序等),並且有幾個屬性表,這些屬性表保存與對象相關的信息,由對象ID鏈接。對象也有其他對象作爲他們的父母,所以這是一個父對象,一個產品,然後它有幾個子對象,這些對象又可以有自己的幾個子對象,以及其他表中的屬性 – TheJubilex 2011-02-24 23:18:40