Case on where clause ORACLE - sql

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))

Related

Update subrequest and limit rownum

I wrote this query:
UPDATE artpxvtemagtemp a
SET
origineprix = 'CEN',
pxvente = (
SELECT
ap.pxvtrpublic
FROM
artpxvte ap
JOIN article ar ON ar.idarticle = ap.idarticle
WHERE
ap.idarticle = a.idarticle
AND a.unite = ap.unite
AND ap.isdegressif = 0
AND ap.idtarifvte = 'T00002'
AND ap.datedebut <= pdateappli
AND ( ap.datefin IS NULL OR pdateappli < ( ap.datefin + 1 ) )
);
But sometimes the sub-query gives more than one result, so the update fails.
I want to order the result and select the first result of the sub-query to make the update.
So I do :
UPDATE artpxvtemagtemp a
SET
origineprix = 'CEN',
pxvente = (
SELECT
pxvtrpublic
FROM
(
SELECT
ap.pxvtrpublic
FROM
artpxvte ap
JOIN article ar ON ar.idarticle = ap.idarticle
WHERE
ap.idarticle = a.idarticle
AND a.unite = ap.unite
AND ap.isdegressif = 0
AND ap.idtarifvte = 'T00002'
AND ap.datedebut <= pdateappli
AND ( ap.datefin IS NULL
OR pdateappli < ( ap.datefin + 1 ) )
ORDER BY
CASE WHEN ap.datefin IS NULL THEN 0 ELSE 1 END ASC,
ap.datefin DESC,
ap.datedebut DESC
)
where rownnum <= 1
);
It does not work because the alias a is lost, so I have to add an additional sub-query just for the rownnum.
Erreur SQL : ORA-00904: "A"."UNITE" : identificateur non valide
00904. 00000 - "%s: invalid identifier"
In Oracle 12C, I don't need a sub-query to fetch the first Nth rows, but I have an old version of Oracle so I can't use this.
How can I make the alias a known?
Use aggregation instead:
pxvente = (select max(ap.pxvtrpublic) keep (dense rank first
order by case when ap.datefin is null then 0 else 1 end,
ap.datefin desc,
ap.datedebut desc
)
from . . .

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);

Column ambiguously defined error. Working without pagination?

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;

How to select results in a row range in HQL?

I am using an oracle dbms and my working query is:
SELECT * from
(
select m.*, rownum r
from Mytable m where m.status in ('PUBLISHED')
)
where r >= 2 and r < 7;
I tried to translate it in HQL:
from
(
select m, rownum r
from Mytable m where m.status in ('PUBLISHED')
)
where r >= :start and r < :end;
but it gives me an error regarding the first "(".
What am I doing wrong?
edit:
in the end I went for a native query but it gives me "invalid column name", that is because I am using the "m" alias. Anyone knows how to solve this?

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