8

我正在尝试创建视图,但出现重复的列名错误。如果我单独运行选择查询,则查询返回如下结果:

SELECT distinct app.APP_REF_NO, app.APP_STATUS, app.APP_DT, app.ATTEND_STAFF,
                app.ATTEND_BRANCH, app.PRODUCT_TYPE, cust.CUST_ID, 
                cust.APP_JOINT_T, cust.ID1_TYPE, cust.ID1, cust.ID2_TYPE, 
                cust.ID2, cust.FIRST_NAME, cust.LAST_NAME, cust.FULL_NAME, 
                cust.FULL_NAME_CAP, cust.DOB, fac.FACILITY_NO, fac.PRODUCT_TYPE,
                fac.PRODUCT_CODE, fac.MAIN_PROD_IND, fac.AMT_APPLIED
FROM 
    LOSA_APP app 
LEFT JOIN 
    LOSA_CUST cust 
ON
    cust.APP_REF_NO = app.APP_REF_NO
LEFT JOIN 
   LOSA_FACILITIES fac 
ON
    fac.APP_REF_NO = app.APP_REF_NO
LEFT JOIN 
    OS_CURRENTSTEP STEP 
ON
    STEP.REF_ID = app.APP_REF_NO
   WHERE (app.APP_STATUS ='P' OR app.APP_STATUS ='T' OR 
         ((app.APP_STATUS='R' OR app.APP_STATUS='S') AND STEP.STEP_NAME='011'));

此查询工作正常。但是当我尝试以如下视图运行它时:

CREATE VIEW basit_test1 AS
SELECT distinct app.APP_REF_NO, app.APP_STATUS, app.APP_DT, app.ATTEND_STAFF,
                app.ATTEND_BRANCH, app.PRODUCT_TYPE, cust.CUST_ID, 
                cust.APP_JOINT_T, cust.ID1_TYPE, cust.ID1, cust.ID2_TYPE, 
                cust.ID2, cust.FIRST_NAME, cust.LAST_NAME, cust.FULL_NAME, 
                cust.FULL_NAME_CAP, cust.DOB, fac.FACILITY_NO, fac.PRODUCT_TYPE,
                fac.PRODUCT_CODE, fac.MAIN_PROD_IND, fac.AMT_APPLIED
FROM 
    LOSA_APP app 
LEFT JOIN 
    LOSA_CUST cust 
ON
    cust.APP_REF_NO = app.APP_REF_NO
LEFT JOIN 
   LOSA_FACILITIES fac 
ON
    fac.APP_REF_NO = app.APP_REF_NO
LEFT JOIN 
    OS_CURRENTSTEP STEP 
ON
    STEP.REF_ID = app.APP_REF_NO
   WHERE (app.APP_STATUS ='P' OR app.APP_STATUS ='T' OR 
         ((app.APP_STATUS='R' OR app.APP_STATUS='S') AND STEP.STEP_NAME='011'));

然后我得到重复的列名错误。为什么我会收到此错误?

4

1 回答 1

19

你有两product_type列:

fac.PRODUCT_TYPE

app.PRODUCT_TYPE

你应该给其中一个起别名,例如

app.PRODUCT_TYPE app_prod_type
于 2013-03-25T10:24:43.937 回答