Column ambiguously defined error. Working without pagination? - sql
I just want to figure whats wrong with below query :-
If we remove the pagination Rownum part it works fine.
Mentioned is the query below:-
We have tried with removing pagination and it works but we are not able
to make it work with pagination and its giving ambiguity error. I can also see all columns are with their tableNames.
--
SELECT *
FROM
(SELECT a.* ,
ROWNUM rnum
FROM
(WITH TEMP_DCS AS (SELECT ACCOUNT_CYCLE_INV_DATE.ACCT_ID, ACCOUNT_CYCLE_INV_DATE.HIERARCHY_SOURCE,
ACCOUNT_CYCLE_INV_DATE.INV_DATE, ACCOUNT_CYCLE_INV_DATE.INVOICE_NUM, ACCOUNT_CYCLE_INV_DATE.DATA_GROUP, ACCOUNT_CYCLE_INV_DATE.BILL_SYSTEM_ID
FROM
ACCOUNT_CYCLE_INV_DATE_302345 ACCOUNT_CYCLE_INV_DATE
WHERE DATA_GROUP=20180118
AND BILL_SYSTEM_ID = 6)
/* Formatted on 4/25/2018 2:26:23 PM (QP5 v5.115.810.9015) */
SELECT
/*+ leading(TEMP_DCS DIM_HIERARCHY FACT_TAX) full(TEMP_DCS) */
DIM_HIERARCHY.ACCT_LBL,
DIM_HIERARCHY.ACCT_DESC,
DIM_HIERARCHY.ACCT_FMT,
DIM_HIERARCHY.ACCT_ID,
DIM_LOCATION.ADDR1,
DIM_LOCATION.ADDR2,
DIM_LOCATION.ADDR3,
DIM_LOCATION.ADDR4,
DIM_LOCATION.ADDR5,
DIM_HIERARCHY.ADMIN_ID,
DIM_HIERARCHY.AGCY_HIER_CD,
DIM_HIERARCHY.AGCY_HIER_NM,
DIM_HIERARCHY.AGGREGATION_ATTR ,
DIM_HIERARCHY.AGGLVLTYPE,
DIM_HIERARCHY.AGGRFMT AS AGGLVLID,
DIM_HIERARCHY.HPID,
DIM_HIERARCHY.AGGRLBL,
DIM_HIERARCHY.AGGR_CUSTOM_LABEL,
REF_BPI_PROVIDER.AREA_ID,
REF_BPI_PROVIDER.BC_DESC,
DIM_HIERARCHY.WORK_CIRCUIT_ID_1,
REF_BPI_PROVIDER.CB_DESC,
REF_BPI_PROVIDER.CHANGE_BY,
REF_BPI_PROVIDER.CHANGE_DT,
FACT_TAX.CHARGE_GROUP_ID,
REF_BPI_PROVIDER.CIC,
DIM_LOCATION.CITY,
FACT_TAX.CNTNUMBER,
REF_BPI_PROVIDER.COMMENTS,
DIM_HIERARCHY.COMM_SVC_AUTH_NB,
FACT_TAX.SUM_AMOUNT_TAX,
FACT_TAX.TAX_GROUP,
DIM_HIERARCHY.FRGN_ACC_1,
DIM_HIERARCHY.FRGN_ACC_2,
DIM_HIERARCHY.GROUPTYPE,
DIM_HIERARCHY.GROUP_LBL,
DIM_HIERARCHY.GROUP_FMT,
DIM_HIERARCHY.GROUP_DESC,
DIM_HIERARCHY.GROUP_ID,
DIM_LOCATION.HS_LOCATION_ID,
DIM_LOCATION.HS_LOCATION_NAME,
FACT_TAX.INTERNATIONAL_TAX_AMT,
DIM_HIERARCHY.INV_CUST_NUM,
FACT_TAX.INVOICE_NUM,
FACT_TAX.INV_DATE,
DIM_HIERARCHY.IP_SRV_ACC_NO,
FACT_TAX.LOCID,
DIM_HIERARCHY.MRK_AREA,
DIM_LOCATION.MOW_CNTRYID,
FACT_TAX.MOW_TAX_PERCENTAGE,
DIM_HIERARCHY.MSTR_ACCT_NBR,
REF_BPI_PROVIDER.ORIG_COMP_CD,
DIM_HIERARCHY.ORIG_SYSACCT_ID,
DIM_HIERARCHY.ORIG_SYSACCT_CD,
DIM_HIERARCHY.PRODUCT_FAMILY_ID,
REF_PRODUCT_FAMILY.PRODUCT_FAMILY,
REF_PRODUCT_FAMILY.PRODUCT_FAMILY_DESC,
TEMP_DCS.ACCT_ID,
TEMP_DCS.HIERARCHY_SOURCE,
TEMP_DCS.INV_DATE,
TEMP_DCS.INVOICE_NUM,
TEMP_DCS.BILL_SYSTEM_ID,
DIM_SUB_ADDL_INFO.SUB_ACCT_ATTR1,
DIM_HIERARCHY.PON,
DIM_HIERARCHY.PORTID,
DIM_HIERARCHY.PORTID1,
DIM_HIERARCHY.PROVIDER_ID,
REF_BPI_PROVIDER.PSEUDO_CIC,
REF_PROD_TYPE.PRODUCT_TYPE_ID,
REF_PROD_TYPE.PRODUCT_TYPE_DESC AS PTDESC,
DIM_HIERARCHY.PRODUCT_TYPE_ID,
DIM_HIERARCHY.RAO,
DIM_HIERARCHY.REGN_CD,
REF_BPI_PROVIDER.SBC_AFFL_IND,
DIM_HIERARCHY.SBU_DESC,
DIM_HIERARCHY.SBU_FMT,
DIM_HIERARCHY.SBU_LBL,
DIM_LOCATION.SERVICE_CTR,
DIM_HIERARCHY.SERVICE_FMT,
DIM_HIERARCHY.SERVICE_FMT2,
DIM_HIERARCHY.SERVICE_FMT3,
DIM_HIERARCHY.SERVICE_FMT4,
DIM_HIERARCHY.SERVICE_ID,
DIM_HIERARCHY.SERVICE_LBL,
DIM_LOCATION.SITE_ALIAS,
DIM_LOCATION.SITE_ID,
DIM_HIERARCHY.SRC_BILLER,
REF_BPI_PROVIDER.STATUS_CD,
REF_STCNTRY.STCNTRYTXT,
FACT_TAX.STCNTRY_ID,
DIM_HIERARCHY.SUBACCTYPE,
DIM_HIERARCHY.SUB_DEP_ID,
DIM_HIERARCHY.SUB_ACCT_ID,
DIM_HIERARCHY.SVCARRTYPE,
REF_BPI_PROVIDER.SVC_PRVDR_DESC,
REF_TAX.TAX_DESC AS TAXDESC,
FACT_TAX.TAX_SURCG_IND,
FACT_TAX.TAX_TYPE_CD,
DIM_HIERARCHY.LEG_TCCUSTID,
REF_TRANS_CODE.TRANS_CODE_DESC,
DIM_HIERARCHY.LEG_TCINVID,
FACT_TAX.TEL_PROVIDER_CD,
FACT_TAX.TOT_TAX_TRANS,
FACT_TAX.TOT_TAX_WITHLD,
DIM_LOCATION.TO_END_USR_CITY,
DIM_LOCATION.TO_END_USR_CUST_NAME,
DIM_LOCATION.TO_END_USR_STATE,
DIM_LOCATION.TO_END_USR_STREET,
DIM_LOCATION.TO_END_USR_ZIP,
FACT_TAX.TRANSCODE_ID,
FACT_TAX.MOW_TAX_WORDING,
DIM_HIERARCHY.WORK_CIRCUIT_ID_2,
DIM_HIERARCHY.WORK_CIRCUIT_ID_3,
DIM_HIERARCHY.WORK_CIRCUIT_ID_4,
DIM_HIERARCHY.WORK_CIRCUIT_ID_5,
DIM_HIERARCHY.WORK_CIRCUIT_ID_6,
DIM_HIERARCHY.WORK_CIRCUIT_ID_7,
DIM_HIERARCHY.WORK_CIRCUIT_ID_8,
DIM_LOCATION.ZIP_CODE,
DIM_HIERARCHY.BUNDLE_FAN,
REF_CHARGE_GROUP.CHARGE_GROUP_ID,
REF_CHARGE_GROUP.CHARGE_GROUP_DESC,
DIM_HIERARCHY.DEP_ID,
DIM_HIERARCHY.DLCI,
DIM_HIERARCHY.DLCI1,
REF_BPI_PROVIDER.EBAT_DESC,
REF_BPI_PROVIDER.EDI_DESC,
DIM_LOCATION.EOL_NUM,
FACT_TAX.CURRENCY_CD,
FACT_TAX.FFP_TAX_INC_IND,
FACT_TAX.BAND,
REF_PROD_TYPE.PRODUCT_TYPE_CODE,
REF_CHARGE_GROUP.CHARGE_GROUP_CODE,
REF_TRANS_CODE.TRANS_CODE,
DIM_HIERARCHY.BILLER_SERVICE,
DIM_LOCATION.LOCATION_ADDRESS
FROM DIM_HIERARCHY,
REF_BPI_PROVIDER,
FACT_TAX,
DIM_LOCATION,
REF_PRODUCT_FAMILY,
TEMP_DCS,
DIM_SUB_ADDL_INFO,
REF_PROD_TYPE,
REF_STCNTRY,
REF_TAX,
REF_TRANS_CODE,
REF_CHARGE_GROUP
WHERE DIM_HIERARCHY.ACCT_ID = TEMP_DCS.ACCT_ID
AND DIM_HIERARCHY.BILL_SYSTEM_ID = TEMP_DCS.BILL_SYSTEM_ID
AND DIM_HIERARCHY.BILLER_SERVICE = TEMP_DCS.HIERARCHY_SOURCE
AND FACT_TAX.INVOICE_NUM =TEMP_DCS.INVOICE_NUM
AND FACT_TAX.BILL_SYSTEM_ID = TEMP_DCS.BILL_SYSTEM_ID
AND FACT_TAX.INVOICE_NUM = TEMP_DCS.INVOICE_NUM
AND FACT_TAX.INV_DATE = TEMP_DCS.INV_DATE
AND DIM_HIERARCHY.BILL_HIERARCHY_ID = FACT_TAX.BILL_HIERARCHY_ID
AND DIM_HIERARCHY.ACCT_PART_KEY = FACT_TAX.ACCT_PART_KEY
AND DIM_HIERARCHY.BILL_SYSTEM_ID = FACT_TAX.BILL_SYSTEM_ID
AND DIM_HIERARCHY.BILL_SYSTEM_ID = REF_PRODUCT_FAMILY.BILL_SYSTEM_ID
AND DIM_HIERARCHY.PRODUCT_FAMILY_ID = REF_PRODUCT_FAMILY.PRODUCT_FAMILY_ID
AND DIM_HIERARCHY.BILL_SYSTEM_ID = REF_PROD_TYPE.BILL_SYSTEM_ID
AND DIM_HIERARCHY.PRODUCT_TYPE_ID = REF_PROD_TYPE.PRODUCT_TYPE_ID
AND DIM_HIERARCHY.BILL_SYSTEM_ID = REF_TRANS_CODE.BILL_SYSTEM_ID
AND FACT_TAX.TRANSCODE_ID = REF_TRANS_CODE.TRANS_CODE_ID
AND DIM_HIERARCHY.BILL_SYSTEM_ID = REF_CHARGE_GROUP.BILL_SYSTEM_ID
AND FACT_TAX.CHARGE_GROUP_ID = REF_CHARGE_GROUP.CHARGE_GROUP_ID
AND FACT_TAX.BILL_SYSTEM_ID = REF_STCNTRY.BILL_SYSTEM_ID
AND FACT_TAX.STCNTRY_ID = REF_STCNTRY.STCNTRY_ID
AND FACT_TAX.TAX_ID = REF_TAX.TAX_ID
AND FACT_TAX.BILL_SYSTEM_ID = REF_TAX.BILL_SYSTEM_ID
AND DIM_HIERARCHY.ACCT_ID = DIM_LOCATION.ACCT_ID (+)
AND DIM_HIERARCHY.BILL_SYSTEM_ID = DIM_LOCATION.BILL_SYSTEM_ID (+)
AND DIM_HIERARCHY.SERVICE_ID = DIM_LOCATION.SERVICE_ID (+)
AND DIM_HIERARCHY.ACCT_PART_KEY = DIM_LOCATION.ACCT_PART_KEY (+)
AND DIM_HIERARCHY.BILL_SYSTEM_ID = REF_BPI_PROVIDER.BILL_SYSTEM_ID (+)
AND DIM_HIERARCHY.PROVIDER_ID = REF_BPI_PROVIDER.SVC_PRVDR_ID (+)
AND DIM_HIERARCHY.ACCT_ID = DIM_SUB_ADDL_INFO.ACCT_ID (+)
AND DIM_HIERARCHY.GROUP_ID = DIM_SUB_ADDL_INFO.GROUP_ID (+)
AND DIM_HIERARCHY.SUB_ACCT_ID = DIM_SUB_ADDL_INFO.SUB_ACCT_ID (+)
AND DIM_HIERARCHY.BILL_SYSTEM_ID = DIM_SUB_ADDL_INFO.BILL_SYSTEM_ID (+)
ORDER BY TEMP_DCS.ACCT_ID, TEMP_DCS.INVOICE_NUM, TEMP_DCS.INV_DATE, DIM_HIERARCHY.GROUP_ID, DIM_HIERARCHY.SUB_ACCT_ID, DIM_HIERARCHY.SERVICE_ID) a) WHERE rnum > 0
and rnum <= 10000
Your inner query has multiple columns with the same names, albeit from different tables; those seem to be:
ACCT_ID (from DIM_HIERARCHY and TEMP_DCS)
CHARGE_GROUP_ID
INV_DATE
INVOICE_NUM
PRODUCT_TYPE_ID
The outer query can't cope with that ambiguity. You need to alias the clashing columns so the names are unique in the outer query.
As a simple demo of the issue:
SELECT *
FROM
(SELECT a.* ,
ROWNUM rnum
FROM
(
-- replacing your entire inner query with somethign simple
select dummy, dummy from dual
) a) WHERE rnum > 0
and rnum <= 10000;
SQL Error: ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
If you alias the columns the issue goes away:
SELECT *
FROM
(SELECT a.* ,
ROWNUM rnum
FROM
(
-- replacing your entire inner query with somethign simple
select dummy as dummy1, dummy as dumym2 from dual
) a) WHERE rnum > 0
and rnum <= 10000;
D D RNUM
- - ----------
X X 1
You want your pagination query (if using rownum) to be in the form of:
select *
from ( select /*+ FIRST_ROWS(n) */
a.*, ROWNUM rnum
from ( your_query_goes_here,
with order by ) a
where ROWNUM <=
:MAX_ROW_TO_FETCH )
where rnum >= :MIN_ROW_TO_FETCH;
You seem to stick both the upper and lower limits in the same place (among other things). You have:
SELECT *
FROM
(SELECT a.* ,
ROWNUM rnum
FROM
(
select ...
ORDER BY TEMP_DCS.ACCT_ID, TEMP_DCS.INVOICE_NUM, TEMP_DCS.INV_DATE, DIM_HIERARCHY.GROUP_ID, DIM_HIERARCHY.SUB_ACCT_ID, DIM_HIERARCHY.SERVICE_ID) a)
WHERE rnum > 0 and rnum <= 10000
You probably want:
select *
from ( select a.*, ROWNUM rnum
from ( select ... from ... ORDER BY ...) a
where ROWNUM <= 10000 )
where rnum >= 0;
Related
How to fix this ORA-00918 error in this SQL code
I have ORA-00918 error with my code and i could not find the problem... the following code gives me this error. ORA-00918 : column ambiguously defined can anyone give me some advice? thanks SELECT * FROM ( SELECT * FROM ( SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B WHERE A.XML_MGS_ID = B.XML_MSG_ID AND A.ERROR_CODE <> '00000000' AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR ) WHERE RNUM BETWEEN CASE WHEN (1-1) != 0 THEN ((1-1)*50)+1 ELSE (1-1)*50 END AND 1*50;
The problem is most probably in second subquery select * SELECT * FROM ( ... subquery C ... ) C, EBILL_USER D WHERE ... AND C.ORIGINATOR = D.ORIGINATOR The table D contains the same columns as the subquery C, for sure the ORIGINATORcolumn Simple change the second query to SELECT C.* and add only the required columns from D. The general aproach how to troubleshoot ORA-00918 is to run the query from the innermost subquery and check that the returned column names are unique. In your case try first, which should be fine SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B WHERE A.XML_MGS_ID = B.XML_MSG_ID AND A.ERROR_CODE <> '00000000' AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' Than run the second innermost subquery SELECT * FROM ( SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B WHERE A.XML_MGS_ID = B.XML_MSG_ID AND A.ERROR_CODE <> '00000000' AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR In your IDE (e.g. SQL Developer) you will see one and more columns with a suffix _1 which is a sign of duplicated column that must be excluded (for columns from the equijoin predicate) or renamed.
you just need to remove outermost query and use C.RNUM instead of RNUM in where clause. Try with below code: SELECT * FROM ( SELECT ROWNUM AS RNUM, A.XML_MSG_ID, A.LOGIN_ID, A.ORIGINATOR, A.RECIPIENT, A.ERROR_CODE, B.DOC_NO, B.DOC_NAME, B.ERROR_MSG FROM XML_MANAGE_TBL A, XML_REFERENCE_TBL B WHERE A.XML_MGS_ID = B.XML_MSG_ID AND A.ERROR_CODE <> '00000000' AND A.XML_MSG_ID >= '20190528' AND (SUBSTR(A.XML_MSG_ID, 1, 8)) <= '20190604' ) C, EBILL_USER D WHERE D.COMP_NUM = '1258169573' AND C.ORIGINATOR = D.ORIGINATOR and (C.RNUM BETWEEN CASE WHEN (1-1) != 0 THEN ((1-1)*50)+1 ELSE (1-1)*50 END AND 1*50);
Case on where clause ORACLE
SELECT * FROM ( SELECT P.PC_ID, PC.PC, P.BLOK_ID, B.BLOK, B.ID_MATERIAL, M.MATERIAL, M.NO_MATERIAL, P.START_DTTM, TO_CHAR(P.START_DTTM,'yyyy-mm-dd') DATE_PERENCANAAN FROM UTSG_PERENCANAAN P INNER JOIN UTSG_PC PC ON P.PC_ID = PC.ID_PC INNER JOIN UTSG_BLOK B ON P.BLOK_ID = B.ID_BLOK LEFT JOIN UTSG_MATERIAL M ON B.ID_MATERIAL = M.ID_MATERIAL WHERE P.NO_LAMBUNG = '341' AND P.LOKASI_ID = '2' AND P.START_DTTM < TO_DATE('2019-01-09 23:40:52', 'yyyy-mm-dd hh24:mi:ss') ORDER BY P.START_DTTM DESC ) WHERE CASE WHEN BLOK = 'DD11' THEN ROWNUM <= 1 ELSE THEN ROWNUM <= 2 END I have query like this, on case in where clause always show error ORA-00905: missing keyword
You can't have the comparison operator within the case statement. Instead, your where clause should be something like: WHERE rownum <= CASE WHEN BLOK = 'DD11' THEN 1 ELSE 2 END
Why use a case expression? WHERE ( (BLOK = 'DD11' AND ROWNUM <= 1) OR ROWNUM <= 2 ) In general it is advised to use "boolean logic" in where clauses, here is an blog of the topic: SQL WHERE clauses: Avoid CASE, use Boolean logic
I think you need to replace where clause to ((BLOK = 'DD11' and ROWNUM <= 1) or (ROWNUM <= 2))
How do I create a conditional SQL query
I am trying to create an Oracle Sql query using IF/Else statements IF EXISTS ( SELECT * FROM baninst1.an_employee_position WHERE baninst1.an_employee_position.person_uid = 593791 AND baninst1.an_employee_position.position_end_date IS NULL) THEN SELECT * FROM baninst1.an_employee_position WHERE baninst1.an_employee_position.person_uid = 593791 AND ( baninst1.an_employee_position.position_end_date IS NULL OR baninst1.an_employee_position.position_end_date > SYSDATE) AND baninst1.an_employee_position.effective_start_date <= SYSDATE;ELSE SELECT * FROM ( SELECT * FROM baninst1.an_employee_position WHERE baninst1.an_employee_position.person_uid = 593791 ) WHERE ROWNUM = 1;END IF; However I receive an "Unknown Command" error when I run it. No more error information
This may provide what you are looking for: SELECT a.* FROM employee_position a where person_uid = 593791 and ( (a.position_end_date is null) or ( a.position_end_date = (select max(position_end_date) from employee_position b where b.person_uid = a.person_uid and b.position_end_date is not null ) ) ) Another way SELECT a.* FROM employee_position a where person_uid = 593791 and ( nvl(a.position_end_date, trunc(sysdate+100)) >= (select max(position_end_date) from employee_position b where b.person_uid = a.person_uid and b.position_end_date is not null ) )
How do I combine subquery rows into one column in Oracle?
I'm working with FileNet. I'm trying to get the folders that a document may be filed in to appear in one column of the record set delimited with semicolons. This was the layout previously decided on and I am tasked with making Oracle do it. Here's what I have for a query so far: SELECT d1.F_DOCNUMBER, d1.F_DOCCLASSNUMBER, d1.F_ENTRYDATE, d1.F_ARCHIVEDATE, d1.F_RETENTBASE, d1.F_RETENTDISP, d1.F_RETENTOFFSET, d1.F_PAGES, d1.F_DOCTYPE, d1.F_DOCFORMAT, d1.A32 AS CERT_NUM, d1.A35 AS DOC_TYPE, d1.A36 AS BATCH_KEY, d1.A37 AS FIELD_REP_CODE, d1.A38 AS EFFECTIVE_DATE, d1.A39 AS VOUCH_NUM_HIGH, d1.A40 AS VOUCH_NUM_LOW, f1.Folders FROM doctaba d1 LEFT JOIN (SELECT SUBSTR (SYS_CONNECT_BY_PATH (F_FOLDERNAME , ';'), 2) Folders FROM (SELECT fc2.F_DOCNUMBER, f2.F_FOLDERNAME, ROW_NUMBER () OVER (ORDER BY f2.F_FOLDERNAME) rn, COUNT (*) OVER () cnt FROM folder_contents fc2 INNER JOIN folder f2 ON f2.F_FOLDERNUMBER = fc2.F_FOLDERNUMBER WHERE fc2.F_DOCNUMBER = d1.F_DOCNUMBER) WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1) f1 ON d1.F_DOCNUMBER = f1.F_DOCNUMBER WHERE d1.F_DOCTYPE IS NULL AND d1.F_DOCNUMBER >= 107777 AND d1.F_DOCNUMBER <= 305791 ORDER BY d1.F_DOCNUMBER; The problem is that d1.F_DOCNUMBER is being marked as an invalid identifier. I read on some forums that Oracle may not let that column identifier work multiple query levels down. Anyone have some suggestions on how to make this work? Thanks! EDIT: Here's my original query that just includes the folder values in rows. SELECT doctaba.F_DOCNUMBER, doctaba.F_DOCCLASSNUMBER, doctaba.F_ENTRYDATE, doctaba.F_ARCHIVEDATE, doctaba.F_RETENTBASE, doctaba.F_RETENTDISP, doctaba.F_RETENTOFFSET, doctaba.F_PAGES, doctaba.F_DOCTYPE, doctaba.F_DOCFORMAT, doctaba.A32 AS CERT_NUM, doctaba.A35 AS DOC_TYPE, doctaba.A36 AS BATCH_KEY, doctaba.A37 AS FIELD_REP_CODE, doctaba.A38 AS EFFECTIVE_DATE, doctaba.A39 AS VOUCH_NUM_HIGH, doctaba.A40 AS VOUCH_NUM_LOW, folder.F_FOLDERNAME FROM doctaba LEFT JOIN folder_contents ON doctaba.F_DOCNUMBER = folder_contents.F_DOCNUMBER INNER JOIN folder ON folder.F_FOLDERNUMBER = folder_contents.F_FOLDERNUMBER WHERE doctaba.F_DOCTYPE IS NULL AND doctaba.F_DOCNUMBER >= 107777 AND doctaba.F_DOCNUMBER <= 17208174 ORDER BY doctaba.F_DOCNUMBER;
In this case, you are lucky. You are only getting one value from the subquery, so you can just make it a correlated subquery in the select clause: SELECT . . . (SELECT SUBSTR(SYS_CONNECT_BY_PATH (F_FOLDERNAME , ';'), 2) as Folders FROM (SELECT fc2.F_DOCNUMBER, f2.F_FOLDERNAME, ROW_NUMBER () OVER (ORDER BY f2.F_FOLDERNAME) rn, COUNT (*) OVER () cnt FROM folder_contents fc2 INNER JOIN folder f2 ON f2.F_FOLDERNUMBER = fc2.F_FOLDERNUMBER WHERE fc2.F_DOCNUMBER = d1.F_DOCNUMBER ) WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = PRIOR rn + 1 ) as Folders FROM doctaba d1 WHERE d1.F_DOCTYPE IS NULL AND d1.F_DOCNUMBER >= 107777 AND d1.F_DOCNUMBER <= 305791 ORDER BY d1.F_DOCNUMBER;
Joining two SELECT statements using Outer Join with multiple alias
I have a complicated select statement that when it is executed, I give it a date or date range I want and the output comes out. The problem is I don't know how to join the same SQL statement with the first Statement having 1 date range and the second statement having another data range. Example below: When I execute the select statement, I choose for the Month of November: EMPLID NAME Current_Gross_Hours(November) When I execute the select statement again, I choose from January to November: EMPLID NAME Year_To_Date_Hours(January - November) What I want: EMPLID NAME Current_Gross_Hours(November) Year_To_Date_Hours(January - November) The SQL Select statement runs correctly if execute by themselves. But I don't know how to join them. Here is the SQL code that I want to write, but I don't know how to write the SQL statement correctly. Any help or direction is greatly appreciated. (SELECT DISTINCT SUM("PSA"."AL_HOURS") AS "Current Gross Hours", "PSJ"."EMPLID","PSP"."NAME" FROM "PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA" WHERE ((("PSA"."CHECK_DT" = TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND ("PSJ"."PAYGROUP" = 'SK2') AND (("PSJ"."EFFSEQ"= ( SELECT MAX("INNERALIAS"."EFFSEQ") FROM "PS_JOB" INNERALIAS WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR" AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID" AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT") AND "PSJ"."EFFDT" = ( SELECT MAX("INNERALIAS"."EFFDT") FROM "PS_JOB" INNERALIAS WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR" AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID" AND "INNERALIAS"."EFFDT" <= SYSDATE))))) AND ("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" ) GROUP BY "PSJ"."EMPLID", "PSP"."NAME" ) AS "Q1" LEFT JOIN (SELECT DISTINCT SUM("PSA"."AL_HOURS") AS "YEAR_TO_DATE Gross Hours", "PSJ"."EMPLID" FROM "PS_JOB" "PSJ", "PS_EMPLOYMENT" "PSE", "PS_PERSONAL_DATA" "PSP", "PS_AL_CHK_HRS_ERN" "PSA" WHERE ((("PSA"."CHECK_DT" BETWEEN TO_DATE('2011-01-01', 'YYYY-MM-DD') AND TO_DATE('2011-11-01', 'YYYY-MM-DD')) AND ("PSJ"."PAYGROUP" = 'SK2') AND (("PSJ"."EFFSEQ"= ( SELECT MAX("INNERALIAS"."EFFSEQ") FROM "PS_JOB" INNERALIAS WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR" AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID" AND "INNERALIAS"."EFFDT" = "PSJ"."EFFDT") AND "PSJ"."EFFDT" = ( SELECT MAX("INNERALIAS"."EFFDT") FROM "PS_JOB" INNERALIAS WHERE "INNERALIAS"."EMPL_RCD_NBR" = "PSJ"."EMPL_RCD_NBR" AND "INNERALIAS"."EMPLID" = "PSJ"."EMPLID" AND "INNERALIAS"."EFFDT" <= SYSDATE))))) AND ("PSJ"."EMPLID" = "PSE"."EMPLID" ) AND ("PSJ"."EMPLID" = "PSP"."EMPLID" ) AND ("PSJ"."FILE_NBR" = "PSA"."FILE_NBR" ) AND ("PSJ"."PAYGROUP" = "PSA"."PAYGROUP" ) AND ("PSE"."EMPLID" = "PSP"."EMPLID" ) GROUP BY "PSJ"."EMPLID" ) AS "Q2" ON "Q1"."EMPLID"="Q2"."EMPLID" ORDER BY "Q1"."NAME"
You are missing a SELECT ... FROM at the start. The AS keyword only works when creating column aliases, not query block aliases. Quotation marks are only necessary for case-sensitive column names - they don't look incorrect in your example but they frequently cause mistakes. SELECT Q1.NAME, ... FROM ( SELECT ... ) Q1 JOIN ( SELECT ... ) Q2 ON Q1.EMPLID=Q2.EMPLID ORDER BY Q1.NAME