我希望下面有足够的信息供您协助
Table A
ID Name Location
A1 John London
A2 Mike London
A3 Adam London
A4 Steve London
A5 James London
Table B
ID TEST Results
A1 TEST1 SATISFACTORY
A1 TEST2 UNSATISFACTORY
A1 TEST3 SATISFACTORY
A2 TEST1 SATISFACTORY
A2 TEST2 UNSATISFACTORY
A2 TEST3 SATISFACTORY
A3 TEST1 SATISFACTORY
A3 TEST2 SATISFACTORY
A4 TEST1 UNSATISFACTORY
A4 TEST2 UNSATISFACTORY
A5 TEST0 NOT TESTED
目前我的查询是:
Select Distinct TableA.ID, TableA.Name, TableA.Loacation, TableB.ID,
TableB.Results
Left outer join TableA on TableA.ID = TableB.ID
因此,当我运行此查询时,我得到以下结果
ID NAME ID Results
A1 JOHN A1 SATISFACTORY
A1 JOHN A1 UNSATISFACTORY
A2 MIKE A2 SATISFACTORY
A2 MIKE A2 UNSATISFACTORY
A3 ADAM A3 SATISFACTORY
A4 STEVE A4 UNSATISFACTORY
A5 JAMES A5 NOT TESTED
因此,如果特定ID的所有测试都是SATISFACTORY(A3),那么我得到1输出 'A3 ADAM A3 SATISFACTORY'
因此,如果特定ID的所有测试都是UNSATISFACTORY(A4),那么我得到1 输出
A4 STEVE A4 UNSATISFACTORY
如果特定ID的所有测试都没有被测试(A5),那么我得到1个输出
A5 STEVE A5 NOT TESTED
虽然如果我的身份证上有一些SATISFACTORY&然后UNSATISFACTORY返回2行
A1 JOHN A1 SATISFACTORY
A1 JOHN A1 UNSATISFACTORY
A2 MIKE A2 SATISFACTORY
A2 MIKE A2 UNSATISFACTORY
我想要实现的是,如果有一个混合的SATISFACTORY& UNSATISFACTORY为1 ID,输出仅显示UNSATISFACTORY。我已经尝试过case case,group by,Joins,MIN / MAX ......只是没有运气
答案 0 :(得分:0)
试试这个,将问题分成3个案例并将各个结果合并在一起:
-- SATISFACTORY & UNSATISFACTORY
SELECT DISTINCT
A.ID,
A.Name,
B.ID,
'SATISFACTORY & UNSATISFACTORY' AS Result
FROM TableA AS A
INNER JOIN TableB AS B on A.ID = B.ID
WHERE B.Results='SATISFACTORY'
AND B.Results='UNSATISFACTORY'
UNION
--SATISFACTORY & NOT UNSATISFACTORY
SELECT DISTINCT
A.ID,
A.Name,
B.ID,
B.Results
FROM TableA AS A
INNER JOIN TableB AS B on A.ID = B.ID
WHERE B.Results='SATISFACTORY'
AND B.Results<>'UNSATISFACTORY'
UNION
--NOT SATISFACTORY & UNSATISFACTORY
SELECT DISTINCT
A.ID,
A.Name,
B.ID,
B.Results
FROM TableA AS A
INNER JOIN TableB AS B on A.ID = B.ID
WHERE B.Results<>'SATISFACTORY'
AND B.Results='UNSATISFACTORY'
答案 1 :(得分:0)
你唯一没有详述的是结果的优先顺序。这个答案假设没有被测试的优势,UNSATISFACTORY胜过SATISFACTORY:
SELECT
table_a.id, table_a.name, b_ranked.test, b_ranked.results
FROM
table_a
LEFT JOIN
(
SELECT
id,
test,
results,
ROW_NUMBER() OVER(PARTITION BY id, test, ORDER BY CASE results WHEN 'NOT TESTED' THEN 0 WHEN 'UNSATISFACTORY' THEN 1 WHEN'SATISFACTORY' THEN 2) as rown
FROM
table_b
) b_ranked
ON table_a.id = b_ranked.id and b_ranked.rown = 1
魔术是通过row_number()调用完成的,它基本上会在具有相同id
,test
的行上放置一个递增计数器,并专门对结果进行排序,因此对您来说最有趣的是始终编号为1
。大多数现代数据库支持这一点,但你的可能不支持(因此我在评论中提到了这一点)。使用这种方法可以避免昂贵的操作将表连接到自身,工会,区别和其他强化的重复数据删除方式
如果你想深入了解它是如何工作的,只需运行内部子查询,然后找出它如何链接到外部
答案 2 :(得分:0)
我认为对于每个ID
- Name
对,您打算获得一条记录,如果至少有一个UNSATISFIED
结果,则该记录为UNSATISFIED
给定ID
和Name
,SATISFIED
如果没有UNSATISFIED
,但至少是SATISFIED
和NOT TESTED
,如果至少有NOT TESTED
B
值。A
请注意,对于left join
中的记录Results
中没有记录的情况,此解决方案不包含行。如果您想要包含该内容,则需要使用NOT TESTED
和默认UNSATISFIED
至SATISFIED
。这里使用的想法是基于观察
'未经测试'&lt; 'SATISFIED'&lt; '不满意'强>
在您的潜在结果中,如果至少有一个UNSATISFIED
,那么它将是最大值。 SATISFIED
只有在给定测试人员不存在NOT TESTED
并且至少发生select A.ID, A.Name, max(B.Results) as Results
from A
join B
on A.ID = B.ID
group by A.ID, A.Name
结果时才能达到最大值。否则,如果有.getElementById("DE_MAN_16").Value = "PT" (or
.getElementById("DE_MAN_16").Value = Range("J21"))
.getElementById("DE_MAN_16").Click
.getElementById("DE_MAN_16").Select
值,那么它将是最大值。
DE_MAN_16