查询问题以获得所需结果

时间:2018-03-08 09:20:36

标签: sql oracle oracle11g

表APPLICATION_STATUS_PROPOSALS

的DDL
CREATE TABLE "USTDMS"."APPLICATION_STATUS_PROPOSALS" 
("EMPNO" VARCHAR2(30 BYTE), 
"COLLEGE_ID" NUMBER(2,0), 
"PROPOSAL_ID" NUMBER(10,0), 
"DEPARTMENT_ID" NUMBER(1,0), 
"APPROVAL_CHAIRMAN" NUMBER(1,0) DEFAULT 0,  
"APPROVED_DATE_CHAIRMAN" DATE, 
"REMARKS_CHAIRMAN" VARCHAR2(300 BYTE),  
"TIMESTAMP_CHAIRMAN" VARCHAR2(30 BYTE), 

表格提案的DDL

 CREATE TABLE "USTDMS"."PROPOSALS" 
("EMPNO" VARCHAR2(30 BYTE), 
"COLLEGE_ID" NUMBER(2,0), 
"PROPOSAL_ID" NUMBER(10,0), 
"DEPARTMENT_ID" NUMBER(1,0), 
"PROJECT_TITLE" VARCHAR2(50 BYTE), 
"PROPONENTS" VARCHAR2(100 BYTE), 
"BIRTH_DATE" DATE, 
"HOME_ADDRESS" VARCHAR2(100 BYTE), 
"BUSINESS_ADDRESS" VARCHAR2(100 BYTE), 
"ACADEMIC_RANK" VARCHAR2(100 BYTE), 
"CONTACT_NO" NUMBER(15,0), 
"COOPERATING_AGENCIES" VARCHAR2(500 BYTE), 
"OBJECTIVES" VARCHAR2(1000 BYTE), 
"PROJECT_SIGNIFICANCE" VARCHAR2(1000 BYTE), 
"PROJECT_STATUS" VARCHAR2(100 BYTE), 
"TOTAL_PROJ_FEE" NUMBER(10,0), 
"RESEARCH_CENTER" VARCHAR2(60 BYTE), 
"SUBMITTED_DATE" DATE, 
"TIMESTAMP" VARCHAR2(30 BYTE), 
"CATEGORY" VARCHAR2(30 BYTE)

所以我目前在数据库中有2个数据,这是我用来获取主席批准状态为1(已批准)的提案的脚本

select p.EMPNO, p.COLLEGE_ID, p.PROPOSAL_ID,p.DEPARTMENT_ID,p.PROJECT_TITLE, 
p.PROPONENTS, p.CATEGORY, p.SUBMITTED_DATE, p.TIMESTAMP,
p.PROJECT_SIGNIFICANCE, p.OBJECTIVES, p.TOTAL_PROJ_FEE from USTDMS.PROPOSALS p, 
USTDMS.APPLICATION_STATUS_PROPOSALS a WHERE p.COLLEGE_ID = a.COLLEGE_ID AND 
p.COLLEGE_ID=26 AND a.APPROVAL_CHAIRMAN = 1;

第一个和第二个数据具有相同的大学ID,但是第一个数据的批准是1而第二个数据是0.结果的查询集仍然是两个数据。如何修复查询以获得所需的结果?

EDITED

Insert into USTDMS.APPLICATION_STATUS_PROPOSALS (EMPNO,COLLEGE_ID,PROPOSAL_ID,DEPARTMENT_ID,
APPROVAL_CHAIRMAN,APPROVED_DATE_CHAIRMAN,REMARKS_CHAIRMAN,TIMESTAMP_CHAIRMAN) values 

第一个数据

('2014072880',26,4818899083,3,1,to_date('03/08/2018','MM/DD/RRRR'),'16:29:44.964');

第二个数据

('2013061644',26,4247805062,1,0,null,null);

我的脚本的初始结果:两个数据都被选中; 我的脚本的期望结果:自批准_Chairman为1(已接受)后,将仅选择第一个数据。

1 个答案:

答案 0 :(得分:0)

还包括EMPNO,PROPOSAL_ID和DEPARTMENT_ID上的联接。

select p.EMPNO, p.COLLEGE_ID, p.PROPOSAL_ID,p.DEPARTMENT_ID,p.PROJECT_TITLE, 
       p.PROPONENTS, p.CATEGORY, p.SUBMITTED_DATE, p.TIMESTAMP,
       p.PROJECT_SIGNIFICANCE, p.OBJECTIVES, p.TOTAL_PROJ_FEE 
  from USTDMS.PROPOSALS p
       inner join
       USTDMS.APPLICATION_STATUS_PROPOSALS a 
    on p.COLLEGE_ID = a.COLLEGE_ID 
   and p.EMPNO = a.EMPNO
   and p.PROPOSAL_ID = a.PROPOSAL_ID
   and p.DEPARTMENT_ID = a.DEPARTMENT_ID
 WHERE p.COLLEGE_ID=26 
   AND a.APPROVAL_CHAIRMAN = 1;