0

我有一张表格,其中列出了会员潜水结果的结果。我需要拿出一个表格,只显示第一次通过潜水测试的个人,并将他们的资格包括在决赛桌中。

我的逻辑是,我会删除任何在考试历史中失败的人以及那些多次出现在列表中的人。

这就是我到目前为止所拥有的。我对 SQL 真的很陌生,所以如果看起来全错了,我真的很抱歉。

谢谢

SELECT b.branchmarina AS "Branch Suburb", m1.memsurname AS "Member Surname",      m1.memfirstname AS "Member First Name", m2.memsurname AS "Mentor Surname", m2.memfirstname     
AS "Mentor First Name", dt.dttestdate AS "Date", dt.DtQualLevel AS "Qualification" /*list of members who passed diving test on first attempt*/
FROM d_branch b, d_divetest dt, d_member m1, d_member m2
WHERE UPPER (dt.dttestresult)='PASS'
AND COUNT(DtMemberNo)<2
AND m2.MemMentorMemberNo=m1.memberno
AND m1.membranchno=b.branchno
AND dt.testresult = (SELECT *
FROM d_divetest dt1
WHERE UPPER (dt1.dttestresult)='PASS'
AND NOT EXISTS (UPPER (dt1.dttestresult)='FAIL'))

这是我正在使用的表:

DtTestDate  DtMemberNo  DtQualLevel DtTestResult    DtReasonFailed
17-Apr-13   201         Beginner    Pass    
18-Apr-13   202         Advanced    Pass    
19-Apr-13   203         Expert      Pass    
20-Apr-13   204         Beginner    Pass    
21-Apr-13   205         Beginner    Fail    Not able to stabilise
26-Apr-13   205         Beginner    Pass    
22-Apr-13   206         Beginner    Fail    Not able to stabilise
27-Apr-13   206         Beginner    Fail    Not able to stabilise
27-May-13   206         Beginner    Pass    
27-Aug-13   206         Advanced    Pass    
23-Apr-13   207         Expert      Pass    
24-Apr-13   208         Beginner    Pass    
25-Apr-13   209         Advanced    Pass    
01-Dec-13   211         Beginner                                             
02-Dec-13   212         Beginner        
03-Dec-13   213         Beginner        
04-Dec-13   214         Beginner        
05-Dec-13   215         Beginner        
27-Apr-13   226         Beginner    Fail    Weight belt not secured correctly
29-Apr-13   227         Advanced    Fail    Unable to complete test
30-Apr-13   228         Beginner    Fail    Not able to stabilise

如果有人有任何好的教程或建议,我将不胜感激。

4

2 回答 2

0

我想你正在寻找这样的东西:

SELECT b.branchmarina AS "Branch Suburb",
  m1.memsurname AS "Member Surname",
  m1.memfirstname AS "Member First Name",
  m2.memsurname AS "Mentor Surname",
  m2.memfirstname AS "Mentor First Name",
  dt.dttestdate AS "Date",
  dt.DtQualLevel AS "Qualification"
FROM d_branch b
INNER JOIN d_member m1 ON m1.membranchno = b.branchno
INNER JOIN d_member m2 ON m1.MemMentorMemberNo = m2.memberno
INNER JOIN d_divetest dt ON dt.MemberNo = m1.membranchno
WHERE UPPER(dt.testresult) = 'PASS'
HAVING COUNT(dt.MemberNo) < 2;

这将确保您在 d_divetest 中只有一个结果并且它是“通过”,其余的 JOINS 是为您提供有关所涉及的分支机构和成员的信息。

旁注:您不能COUNT(DtMemberNo)<2 在 WHERE 子句中使用。您必须在 HAVING 子句中验证这一点。

于 2013-11-04T10:48:45.087 回答
0

更改它以满足您的需求:

SELECT b.branchmarina  AS "Branch Suburb",
       m1.memsurname   AS "Member Surname",
       m1.memfirstname AS "Member First Name",
       m2.memsurname   AS "Mentor Surname",
       m2.memfirstname AS "Mentor First Name",
       dt.dttestdate   AS "Date",
       dt.DtQualLevel  AS "Qualification" /*list of members who passed diving test on first attempt*/
FROM 
    d_branch b
INNER JOIN 
    d_member m1
ON 
    m1.membranchno = b.branchno
INNER JOIN 
    d_member m2
ON
    m2.MemMentorMemberNo = m1.memberno
INNER JOIN 
    d_divetest dt
ON 
    m2.memberno = dt.dtMemberNo
WHERE UPPER(dt.dttestresult) = 'PASS'
   AND (select count(*) from d_divetest dd where dd.dtMemberNo = dt.MemberNo)=1
于 2013-11-04T10:34:52.787 回答