我有一张表格,其中列出了会员潜水结果的结果。我需要拿出一张桌子,只显示第一次通过跳水测试的人,并将他们的资格列入决赛桌。
我的逻辑是,我删除了那些在考试历史中失败的人以及那些出现在列表中的人多一次。
这是我到目前为止所拥有的。我对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
如果有人有任何好的教程或建议,我将不胜感激。
答案 0 :(得分: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
答案 1 :(得分: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中只有一个结果,并且它是'PASS',其余的JOINS是为您提供有关分支机构和成员的信息。
旁注:您不能在WHERE子句中使用COUNT(DtMemberNo)<2
。您必须在HAVING子句中验证它。