I have a table that looks for a row with eff_dte = WK_BCC_DATES. WK_BCC_DATES is a variable computed in a PL1 program, now I need this computation done in one query so QMF will do the computation. The computation is WK_BCC_DATES = DTE1 + NO_DAYS.
SELECT SUBSTR(PARM_VALUE,1,10)
FROM BCD75DBA.BCCA6000 T60
WHERE T60.COUNTRY_CODE = '896'
AND T60.SUBSIDIARY_CODE = '01'
AND T60.PARM_NAME = 'BCC_DATES'
AND T60.EFF_DTE = (SELECT MAX(T60A.EFF_DTE)
FROM BCD75DBA.BCCA6000 T60A
WHERE T60A.COUNTRY_CODE = '896'
AND T60A.SUBSIDIARY_CODE = '01'
AND T60A.PARM_NAME = 'BCC_DATES')`
and
SELECT SUBSTR(PARM_VALUE,1,3)
FROM BCD75DBA.BCCA6000 T60
WHERE T60.COUNTRY_CODE = '896'
AND T60.SUBSIDIARY_CODE = '01'
AND T60.PARM_NAME = 'BCC_DAYS'
AND T60.EFF_DTE = (SELECT MAX(T60A.EFF_DTE)
FROM BCD75DBA.BCCA6000 T60A
WHERE T60A.COUNTRY_CODE = '896'
AND T60A.SUBSIDIARY_CODE = '01'
AND T60A.PARM_NAME = 'BCC_DAYS')`
I tried grouping the first query AS DTE1 and then the second AS NO_DAYS but I am having an error "not valid in the context it is used".
Please advise what else I can do. I am using DB2 v9. Thanks.
So, you want to combine these queries? That's actually pretty easy. It's a little hard to tell what was causing the error without your combined query, but you may have been putting the alias out of place.
(For future reference, your tables appear to be some for of an EAV - Entity Attribute Value. Use that as a search term for future queries)
I think a slight re-writing will help you out here:
WITH Most_Recent_Rows AS (SELECT parm_name, parm_value,
ROW_NUMBER() OVER(PARTITION BY parm_name
ORDER BY eff_dte DESC) AS rn
FROM BCD75DBA.BCCA6000
WHERE country_code = '896'
AND subsidiary_code = '01'
AND parm_name IN ('BCC_DAYS', 'BCC_DATES'))
SELECT CAST(SUBSTR(bcc_dates.parm_value, 1, 10) AS DATE) +
CAST(SUBSTR(bcc_days.parm_value, 1, 3) AS INTEGER) DAYS
FROM Most_Recent_Rows bcc_days
JOIN Most_Recent_Rows bcc_dates
ON bcc_dates.parm_name = 'BCC_DATES'
AND bcc_dates.rn = 1
WHERE bcc_days.parm_name = 'BCC_DAYS'
AND bcc_days.rn = 1
Incidentally, I think you were trying for something like the following:
SELECT bcc_days, bcc_dates
FROM (SELECT SUBSTR(rw.parm_value, 1, 3) AS bcc_days
FROM BCD75DBA.BCCA6000 rw
JOIN (SELECT country_code, subsidiary_code, parm_name, MAX(eff_date) AS eff_date
FROM BCD75DBA.BCCA6000
WHERE parm_name = 'BCC_DAYS'
GROUP BY country_code, subsidiary_code, parm_name) ref
ON ref.country_code = rw.country_code
AND ref.subsidiary_code = rw.subsidiary_code
AND ref.parm_name = rw.parm_name
AND ref.eff_date = rw.eff_date) bcc_days
CROSS JOIN (SELECT SUBSTR(rw.parm_value, 1, 10) AS bcc_dates
FROM BCD75DBA.BCCA6000 rw
JOIN (SELECT country_code, subsidiary_code, parm_name, MAX(eff_date) AS eff_date
FROM BCD75DBA.BCCA6000
WHERE parm_name = 'BCC_DATES'
GROUP BY country_code, subsidiary_code, parm_name) ref
ON ref.country_code = rw.country_code
AND ref.subsidiary_code = rw.subsidiary_code
AND ref.parm_name = rw.parm_name
AND ref.eff_date = rw.eff_date) bcc_dates
This is less ideal, due to the repletion in the sub-subqueries. This can be combined with the previous part of the answer (using a CTE for the GROUP BY, then joining twice). I'm not sure which approach will yield better performance actually.
Neither query has been tested, due to lack of sample data and desired results, and the fact I don't currently have an instance.
Related
Problem
So the situation that I am facing here with this SQL Query, is that it is taking about 12 seconds to run making the screen super slow.
Goal
Do the necessary changes in order to improve the performance and make it faster. I was thinking about instead of the OR in the Where clause to use the UNION?
SELECT Tool.*, Interview.*
FROM Tool
INNER JOIN Interview ON Interview.Id = Tool.InterviewId
WHERE (Tool.ToolTypeId = #ToolTypeId
AND Tool.Is_Active = 1
AND Tool.InterviewId = #InterviewId
AND Tool.ToolId = #ToolId
AND Tool.CustomerId = #CustomerId)
OR Tool.Id = (
SELECT TOP 1 SubTool.Id
FROM Tool SubTool
INNER JOIN Interview subInterview ON subInterview.Id = SubTool.ToolId
WHERE SubTool.ToolTypeId = #ToolTypeId
AND SubTool.Is_Active = 1
AND SubTool.InterviewId != #InterviewId
AND SubTool.ToolId = #ToolId
AND subTool.CustomerId = #CustomerId
AND convert(datetime, subTool.DateTime, 120) < #ToolDateTime
ORDER BY subTool.DateTime DESC, subTool.StartDate DESC,
subTool.EndDate, subTool.Id DESC
)
ORDER BY Tool.StartDate, Tool.Id
NOTE: I believe the actual query output is not necessary in this case, since we are looking for some structural issues that might be impacting the performance.
I would suggest rephrasing the query to eliminate the subquery in the WHERE clause.
If you are looking for one row in the result set regardless of conditions, you can use:
SELECT TOP (1) Tool.*, Interview.*
FROM Tool JOIN
Interview
ON Interview.Id = Tool.InterviewId
WHERE Tool.ToolTypeId = #ToolTypeId AND
Tool.Is_Active = 1
Tool.ToolId = #ToolId AND
Tool.CustomerId = #CustomerId AND
(Tool.InterviewId = #InterviewId OR
convert(datetime, Tool.DateTime, 120) < #ToolDateTime
)
ORDER BY (CASE WHEN Tool.InterviewId = #InterviewId THEN 1 ELSE 2 END),
Tool.DateTime DESC, sTool.StartDate DESC, Tool.EndDate, Tool.Id DESC;
Your final ORDER BY suggests that you are expecting more than one row for the first condition. So, you can use a subquery and window functions:
SELECT ti.*
FROM (SELECT Tool.*, Interview.*,
COUNT(*) OVER (PARTITION BY (CASE WHEN Tool.InterviewId = #InterviewId THEN 1 ELSE 0 END)) as cnt_interview_match,
ROW_NUMBER() OVER (ORDER BY subTool.DateTime DESC, subTool.StartDate DESC, subTool.EndDate, subTool.Id DESC) as seqnum
FROM Tool JOIN
Interview
ON Interview.Id = Tool.InterviewId
WHERE Tool.ToolTypeId = #ToolTypeId AND
Tool.Is_Active = 1
Tool.ToolId = #ToolId AND
Tool.CustomerId = #CustomerId AND
(Tool.InterviewId = #InterviewId OR
convert(datetime, Tool.DateTime, 120) < #ToolDateTime
)
) ti
WHERE InterviewId = #InterviewId OR
(cnt_interview_match = 0 AND seqnum = 1);
Note that the subquery requires that the columns have different names, so you might need to fiddle with that.
Then, you want an index on TOOL(ToolTypeId, Is_Active, ToolId, CustomerId, InterviewId, DateTime). I assume that Interview(Id) is already indexed as the primary key of the table.
My interpretation of what your query does (and by inference, what you want it to do) is different from #GordonLinoff's.
Gordon's queries could be paraphrased as...
If there are rows with Tool.InterviewId = #InterviewId...
Return those rows and only those rows
If there are no such rows to return...
Return the latest row for other InterviewId's
My understanding of what your query actually does is that you want both possibilities returned together, at all times.
This ambiguity is an example of why you really should include example data.
For example, this would be a re-wording or your existing query (using UNION as per your own suggestion)...
WITH
filter_tool_customer AS
(
SELECT Tool.*, Interview.*
FROM Tool
INNER JOIN Interview ON Interview.Id = Tool.InterviewId
WHERE Tool.ToolTypeId = #ToolTypeId
AND Tool.Is_Active = 1
AND Tool.ToolId = #ToolId
AND Tool.CustomerId = #CustomerId
),
matched AS
(
SELECT *
FROM filter_tool_customer
WHERE InterviewId = #InterviewId
),
mismatched AS
(
SELECT TOP 1 *
FROM filter_tool_customer
WHERE InterviewId <> #InterviewId
AND DateTime < CONVERT(VARCHAR(20), #ToolDateTime, 120)
ORDER BY DateTime DESC,
StartDate DESC,
EndDate,
Id DESC
),
combined AS
(
SELECT * FROM matched
UNION ALL
SELECT * FROM mismatched
)
SELECT * FROM combined ORDER BY StartDate, Id
I am trying to update a SQL table from a remote DB2 table. There may be multiple updates for the same record but I need the updates to happen in the order they are in in the DB2 table. You can not use Order By on a derived table. I have tried several different options to try to get this to work, but the updates still do not happen in order.
For example:
Change 1 - CUSTOMER NAME = ABCX
Change 2 - CUSTOMER NAME = ABC
After I run the query, the customer name is ABCX when it should be ABC.
I truly do not know what else to try. I've tried temp tables (still derived table), creating a concatenated field with date and time fields, sub-select, row_number() over(order by date, time) and many other things. I'd like to keep it in order by the date and time fields in the remote table.
Any insight would be appreciated.
Thank you.
Here is the basic code I have:
SET
A.CUSRID = B.RECORD_ID,
A.CUSSTS = B.ACTIVE_CODE,
A.CUSCOM = B.COMPANY_NUMBER,
A.CUSMNM = B.CUSTOMER_NAME,
A.CUSAD1 = B.CUSTOMER_ADDRESS_1,
A.CUSAD2 = B.CUSTOMER_ADDRESS_2,
A.CUSAD3 = B.CUSTOMER_ADDRESS_3,
A.CUSZIP = B.CUSTOMER_ZIP_CODE,
A.CUSZPE = B.CUSZPE_NOT_USED,
A.CUSSTC = B.CUSTOMER_STATE,
A.CUSARA = B.CUSTOMER_AREA_CODE,
A.CUSPHN = B.CUSTOMER_PHONE,
A.CUSB17 = B.CUSB17_NOT_USED,
A.CUSTMT = B.STATEMENT_PRINT_CODE,
A.CUSCRL = B.CREDIT_LIMIT,
A.CUSCRC = B.CREDIT_CODE,
A.CUSMCD = B.CUSMCD_NOT_USED,
A.CUSTX1 = B.TAX_RATE_1,
A.CUSTX2 = B.TAX_RATE_2,
A.CUSTXC = B.TAX_RATE,
A.CUSTXE = B.TAX_EXEMPT_ID,
A.CUSB48 = B.CUSB48_NOT_USED,
A.CUSMDT = B.MAINTENANCE_DATE,
A.CUSB20 = B.CUSB20_NOT_USED,
A.CSSRCH = B.SEARCH_FIELD,
A.CUSBRN = B.BRANCH_ID,
A.CUSDST = B.DISTRIBUTOR_NUMBER,
A.CUSB28 = B.CUSB28_NOT_USED
FROM
dbo.mcusmas A
INNER JOIN (
SELECT
RECORD_ID,
ACTIVE_CODE,
COMPANY_NUMBER,
CUSTOMER_NUMBER,
CUSTOMER_NAME,
CUSTOMER_ADDRESS_1,
CUSTOMER_ADDRESS_2,
CUSTOMER_ADDRESS_3,
CUSTOMER_ZIP_CODE,
CUSZPE_NOT_USED,
CUSTOMER_STATE,
CUSTOMER_AREA_CODE,
CUSTOMER_PHONE,
CUSB17_NOT_USED,
STATEMENT_PRINT_CODE,
CREDIT_LIMIT,
CREDIT_CODE,
CUSMCD_NOT_USED,
TAX_RATE_1,
TAX_RATE_2,
TAX_RATE,
TAX_EXEMPT_ID,
CUSB48_NOT_USED,
MAINTENANCE_DATE,
CUSB20_NOT_USED,
SEARCH_FIELD,
BRANCH_ID,
DISTRIBUTOR_NUMBER,
CUSB28_NOT_USED,
FROM remoteserver.MCUSMASPLG
WHERE Event_State_ID = '*New' AND SENT_TO_DATA_WAREHOUSE = 'N'
) B
ON A.CUSMNB = B.CUSTOMER_NUMBER
There is no "change 1" or "change 2". There are only rows and SQL Server arbitrarily ends up using one of them. If you want to control the rows, you should select the one you want in advance:
FROM dbo.mcusmas A JOIN
(SELECT B.*,
ROW_NUMBER() OVER (PARTITION BY CUSTOMER_NUMBER ORDER BY <ordering col>) as seqnum
FROM remoteserver.MCUSMASPLG
WHERE Event_State_ID = '*New' AND
SENT_TO_DATA_WAREHOUSE = 'N'
) B
ON A.CUSMNB = B.CUSTOMER_NUMBER
I don't know how you are determining which row is the right one. Presumably, some column has this information and you can use it in the ORDER BY.
Two tables store different properties for each product: CTI_ROUTING_VIEW and ORD_MACH_OPS
They are both organized by SPEC_NO > MACH_SEQ_NO but the format of the Sequence number is different for each table so it can't be used for a JOIN. ORCH_MACH_OPS has MACHINE and PASS_NO, meaning if a product goes through the same machine twice, the row with the higher SEQ_NO will be PASS_NO 2, 3, etc. CTI_ROUTING_VIEW does not offer PASS_NO, but I can achieve the desired result with:
SELECT TOP (1000) [SPEC_NO]
,[SPEC_PART_NO]
,[MACH_NO]
,[MACH_SEQ_NO]
,[BLANK_WID]
,[BLANK_LEN]
,[NO_OUT_WID]
,[NO_OUT_LEN]
,[SU_MINUTES]
,[RUN_SPEED]
,[NO_COLORS]
,[PRINTDIEID]
,[CUTDIEID]
,ROW_NUMBER() OVER (PARTITION BY MACH_NO ORDER BY MACH_SEQ_NO) as PASS_NO
FROM [CREATIVE].[dbo].[CTI_ROUTING_VIEW]
I would think that I could use this artificial PASS_NO as a JOIN condition, but I can't seem to get it to come through. This is my first time using ROW_NUMBER() so I'm just wondering if I'm doing something wrong in the JOIN syntax.
SELECT rOrd.[SPEC_NO]
,rOrd.[MACH_SEQ_NO]
,rOrd.[WAS_REROUTED]
,rOrd.[NO_OUT]
,rOrd.[PART_COMP_FLG]
,rOrd.[SCHED_START]
,rOrd.[SCHED_STOP]
,rOrd.[MACH_REROUTE_FLG]
,rOrd.[MACH_DESCR]
,rOrd.REPLACED_MACH_NO
,rOrd.MACH_NO
,rOrd.PASS_NO
,rWip.MAX_TRX_DATETIME
,ISNULL(rWip.NET_FG_SUM*rOrd.NO_OUT,0) as NET_FG_SUM
,CASE
WHEN rCti.BLANK_WID IS NULL then 'N//A'
ELSE CONCAT(rCti.BLANK_WID, ' X ', rCti.BLANK_LEN)
END AS SIZE
,ISNULL(rCti.PRINTDIEID,'N//A') as PRINTDIEID
,ISNULL(rCti.CUTDIEID, 'N//A') as CUTDIEID
,rStyle.DESCR as STYLE
,ISNULL(rCti.NO_COLORS, 0) as NO_COLORS
,CAST(CONCAT(rOrd.ORDER_NO,'-',rOrd.ORDER_PART_NO) as varchar) as ORD_MACH_KEY
FROM [CREATIVE].[dbo].[ORD_MACH_OPS] as rOrd
LEFT JOIN (SELECT DISTINCT
[SPEC_NO]
,[SPEC_PART_NO]
,[MACH_NO]
,MACH_SEQ_NO
,[BLANK_WID]
,[BLANK_LEN]
,[NO_COLORS]
,[PRINTDIEID]
,[CUTDIEID]
,ROW_NUMBER() OVER (PARTITION BY MACH_NO ORDER BY MACH_SEQ_NO) as PASS_NO
FROM [CREATIVE].[dbo].[CTI_ROUTING_VIEW]) as rCti
ON rCti.SPEC_NO = rOrd.SPEC_NO
and rCti.MACH_NO =
CASE
WHEN rOrd.REPLACED_MACH_NO is null then rOrd.MACH_NO
ELSE rOrd.REPLACED_MACH_NO
END
and rCti.PASS_NO = rOrd.PASS_NO
LEFT JOIN INVENTORY_ITEM_TAB as rTab
ON rTab.SPEC_NO = rOrd.SPEC_NO
LEFT JOIN STYLE_DESCRIPTION as rStyle
ON rStyle.DESCR_CD = rTab.STYLE_CD
LEFT JOIN (
SELECT
JOB_NUMBER
,FORM_NO
,TRX_ORIG_MACH_NO
,PASS_NO
,SUM(GROSS_FG_QTY-WASTE_QTY) as NET_FG_SUM
,MAX(TRX_DATETIME) as MAX_TRX_DATETIME
FROM WIP_MACH_OPS
WHERE GROSS_FG_QTY <> 0
GROUP BY JOB_NUMBER, FORM_NO, TRX_ORIG_MACH_NO, PASS_NO) as rWip
ON rWip.JOB_NUMBER = rOrd.ORDER_NO
and rWip.FORM_NO = rOrd.ORDER_PART_NO
and rWip.TRX_ORIG_MACH_NO = rOrd.MACH_NO
and rWip.PASS_NO = rOrd.PASS_NO
WHERE rOrd.SCHED_START > DATEADD(DAY, -20, GETDATE())
I fixed it by adding a second partition.
ROW_NUMBER() OVER (PARTITION BY SPEC_NO, MACH_NO ORDER BY MACH_SEQ_NO) as PASS_NO
Looking for a better way to write this query and my SQL skills aren't great, basic really so looking for any pointers to make this better. This is only the first two columns and the full report will have a further 10.
I'm taking a specific set of repair types and doing analysis on them with counts and calculations. The 1st is jobs brought forward to the current financial year and the second is total amount of jobs currently received.
SELECT
"Type",
(
SELECT
NVL (COUNT(jjo.jjobno), 0)
FROM
jjobh jjo
WHERE
jjo.jclcode = 'L'
AND jjo.jstatus <> '6'
AND jjo.year_rec <> (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
AND (
week_comp IS NULL
OR year_comp = (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
)
AND jjo.jrepair_type = "Type"
) AS "B/F",
(
SELECT
NVL (COUNT(jjo.jjobno), 0)
FROM
jjobh jjo
WHERE
jjo.jclcode = 'L'
AND jjo.jstatus <> '6'
AND jjo.year_rec = (
SELECT
sub_code
FROM
code_table
WHERE
main_code = 'YEAR'
)
AND jjo.jrepair_type = "Type"
) AS "Recvd"
FROM
(
SELECT
rep.repair_type_code AS "Type"
FROM
repair_type rep
WHERE
rep.client = 'L'
AND rep.work_centre = '004682'
ORDER BY
rep.repair_type_code
)
ORDER BY
"Type";
Your code is a mess. I suspect you want something like:
SELECT jjo.jrepair_type, count(*) as valbf
FROM (SELECT coalesce(COUNT(jjo.jjobno), 0)
FROM jjobh jjo cross join
(SELECT sub_code
FROM code_table
WHERE main_code = 'YEAR'
) sc
WHERE jjo.jclcode = 'L' AND
jjo.jstatus <> '6' AND
jjo.year_rec <> sc.sub_code AND
(week_comp IS NULL OR
year_comp = sc.sub_code
)
) jjo join
(SELECT rep.repair_type_code AS "Type"
FROM repair_type rep
WHERE rep.client = 'L' AND
rep.work_centre = '004682'
) rtc
on jjo.jrepair_type = rtc.repair_type_code
group by jjo.jrepair_type;
It looks like you want to join the "jjo" table to the "repair type code" table, producing information about each repair type. The order by in the subquery is useless.
My suggestion is to move the "jjo" table to the outer "from". You should also move the WHERE clauses to the outermost WHERE clause (which I didn't do). I haven't quite figured out the date logic, but this might get you on the right track.
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