我必须确定任何容器组是否在具有任何结果的多个组中进行测试。
我觉得我需要一个简单的循环,但不能在我的脑海中构建它。有一组4个表通过测试连接容器中的所有点,然后到结果。
表con_group1
包含 container_groups 。表containers2
包含 container_group_ids 和各个 container_ids 和 test_group ID。表test_groups3
包含 test_group_ids 和 test_ids ,表tests4
包含 test_ids 和 result_id 。 Table 5
包含结果。
我需要确定哪些容器都有测试结果。无论结果如何,只要容器中包含的每个测试都有结果。
------------------------------------
| Container | Test | Result |
------------------------------------
| 8 | 69 | |
| 9 | 87 | 8 |
| 8 | 45 | |
| 6 | 58 | |
| 9 | 95 | 5 |
------------------------------------
期望的结果:
-------------------------
| Container Group | |
-------------------------
| 2 | done |
| 1 | open |
| 3 | open |
-------------------------
{CREATE TABLE CONTAINERS2
(CON_GROUP NUMBER(10),
CONTAINER NUMBER(10));
CREATE TABLE CON_GROUP1
(CON_GROUP NUMBER(10));
CREATE TABLE TESTS4
(RESULT_ID NUMBER(10),
TEST NUMBER(10));
CREATE TABLE TEST_GROUPS3
(CONTAINER NUMBER(10),
TEST NUMBER(10));
INSERT INTO CONTAINERS2(CON_GROUP, CONTAINER) VALUES (1, 8);
INSERT INTO CONTAINERS2(CON_GROUP, CONTAINER) VALUES (3, 6);
INSERT INTO CONTAINERS2(CON_GROUP, CONTAINER) VALUES (2, 9);
INSERT INTO CON_GROUP1(CON_GROUP) VALUES(1);
INSERT INTO CON_GROUP1(CON_GROUP) VALUES(2);
INSERT INTO CON_GROUP1(CON_GROUP) VALUES(3);
INSERT INTO TESTS4(RESULT_ID, TEST) VALUES (8, 87);
INSERT INTO TESTS4 (RESULT_ID, TEST) VALUES (NULL, 45);
INSERT INTO TESTS4 (RESULT_ID, TEST) VALUES(NULL, 58);
INSERT INTO TESTS4 (RESULT_ID, TEST) VALUES (NULL, 69);
INSERT INTO TESTS4 (RESULT_ID, TEST) VALUES(5, 95);
INSERT INTO TEST_GROUPS3(CONTAINER, TEST) VALUES(8, 45);
INSERT INTO TEST_GROUPS3 (CONTAINER, TEST) VALUES(9, 87);
INSERT INTO TEST_GROUPS3 (CONTAINER, TEST) VALUES(6, 58);
INSERT INTO TEST_GROUPS3(CONTAINER, TEST) VALUES (8, 69);
INSERT INTO TEST_GROUPS3(CONTAINER, TEST) VALUES (9, 95);}
答案 0 :(得分:1)
一系列LEFT JOIN和GROUP BY可以完成这项工作:
SELECT cg.con_group,
max( CASE WHEN result_id IS NULL
THEN 'Open' ELSE 'Closed'
END ) As Result
FROM con_group1 cg
LEFT JOIN CONTAINERS2 co ON co.con_group = cg.con_group
LEFT JOIN TEST_GROUPS3 tg ON tg.container = co.container
LEFT JOIN TESTS4 t4 ON t4.TEST = tg.TEST
GROUP BY cg.con_group
演示:SQLFiddle
| con_group | result |
|-----------|--------|
| 1 | Open |
| 3 | Open |
| 2 | Closed |
...需要确定哪些容器组符合条件,而不是容器.. 容器必须有资格获得容器组 资格..
如果我理解这个要求,你需要:
SELECT cg.con_group,
co.CONTAINER,
max( CASE WHEN result_id IS NULL
THEN 'Open' ELSE 'Closed'
END ) As Result
FROM con_group1 cg
LEFT JOIN CONTAINERS2 co ON co.con_group = cg.con_group
LEFT JOIN TEST_GROUPS3 tg ON tg.container = co.container
LEFT JOIN TESTS4 t4 ON t4.TEST = tg.TEST
GROUP BY cg.con_group,
co.CONTAINER
演示:SQLFiddle
| con_group | container | result |
|-----------|-----------|--------|
| 1 | 8 | Open |
| 2 | 9 | Closed |
| 3 | 6 | Open |
答案 1 :(得分:0)
您需要计算每个容器的测试次数和每个容器的结果测试次数,并比较两个结果集。此解决方案使用子查询来获取两个计数和一个外部联接(因为可能一组测试根本没有结果):
with tests as
( select tg.container
, count(tg.test) as tst_cnt
from test_groups3 tg
group by tg.container )
, results as
( select tg.container
, count(tr.test) as tst_cnt
from test_groups3 tg
join tests4 tr
on tr.test = tg.test
group by tg.container )
select tests,container
, case when tests.tst_cnt = results.tst_cnt
then 'done'
else 'open'
end as status
from tests
left join results
on tests.constainer = results.container
/