Parenthesis error when I add one more UNION [duplicate] - sql
This question already has answers here:
Oracle: Order by Union returning ORA-00933: SQL command not properly ended
(3 answers)
Closed 4 months ago.
I have a subquery of another query in which I have to show a lot of data, and I wanted to add a third UNION in the query. It should be noted that before adding the third UNION the query worked without problems, but when adding the third UNION it shows me an error of
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
The line it indicates is a parenthesis just before the third UNION, but if I add it, everything falls apart and gives an error that the query is not closing properly and thus a loop. Searching I have seen that it may be because of the order by that does not support them when there are multiple unions, but with two it already works, I do not understand it.
This is the entire subquery:
SELECT
*
FROM
(
SELECT
ROWNUM RNUM,
SUBQUERY.*
FROM
(
SELECT
SERVICES_TABLE.SERVICE_ID SESSION_SERVICE_CATALOG_ID,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_3_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_3_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_2_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_2_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.ID MT_CES_SERVICE_LEVEL_2_ID,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_1_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_1_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_1.ID MT_CES_SERVICE_LEVEL_1_ID,
MT_CES_SERVICE_LEVEL_3.ALARM_DAYS CES_CATALOG_ALARM_DAYS,
MT_CES_SERVICE_LEVEL_3.ADVANCE_DAYS CES_CATALOG_ADVANCE_DAYS,
MT_CES_SERVICE_LEVEL_3.COST_BY_STUDENT CES_CATALOG_COST_BY_STUDENT,
MT_CES_SERVICE_LEVEL_3.DESCRIPTION,
SERVICES_TABLE.COST SESSION_SERVICE_COST,
SERVICES_TABLE.NUM_ITEMS SESSION_SERVICE_NUM_ITEMS,
SERVICES_TABLE.COMMENTS SESSION_SERVICE_COMMENTS,
SERVICES_TABLE.STATUS PROGRAM_SERVICE_STATUS,
MT_CES_STATUS.TEXT_NAME_EN PROGRAM_SERVICE_STATUS_LITERAL,
(
SELECT
LISTAGG(MTP.TEXT_NAME_EN, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_NAME_EN ASC
)
FROM
FRM.MT_CES_SERVICE_LEVEL_3_PROVIDERS P
INNER JOIN FRM.MT_CES_PROVIDER MTP
ON
(MTP.ID = P.PROVIDER_ID)
WHERE
P.MT_CES_SERVICE_LEVEL_3_ID = MT_CES_SERVICE_LEVEL_3.ID
) PROVIDER_LITERAL,
(
SELECT
LISTAGG(PRO.NAME, ', ') WITHIN GROUP(
ORDER BY
PRO.NAME ASC
)
FROM
FRM.SESIONCURSO SES
INNER JOIN FRM.AS_COURSE COU
ON
(
SES.CURSO = COU.ID AND COU.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD PE
ON
(
COU.PERIOD_ID = PE.ID AND PE.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM PRO
ON
(
PE.PROGRAM_ID = PRO.ID AND PRO.REMOVED = '0'
)
WHERE
PRO.ID != AS_PROGRAM.ID AND SES.HORAINICIO = SE.HORAINICIO AND(
NVL(
SE.AULA,
(
NVL(
AS_COURSE.CLASSROOM_ID,
AS_PROGRAM.CLASSROOM_ID
)
)
)
) =(
NVL(
SES.AULA,
(
NVL(
COU.CLASSROOM_ID,
PRO.CLASSROOM_ID
)
)
)
)
) PROGRAMS_NAME,
AS_PROGRAM.PARTICIPANTS PROGRAM_PARTICIPANTS,
SE.HORAINICIO SESSION_DATE,
SE.ID SESSION_ID,
'0' IS_SPECIAL_SESSION,
SE.NOMBRE SESSION_TITLE,
SERVICES_TABLE.ID SERVICE_ID,
SERVICES_TABLE.PARTICIPANTS,
SERVICES_TABLE.CLASSROOM_ID SERVICE_CLASSROOM_ID,
SERVICES_TABLE.SERVICE_START,
SERVICES_TABLE.SERVICE_END,
(
SELECT
AUL.TEXT_CODE
FROM
FRM.MT_CLASSROOM AUL
WHERE
AUL.ID = NVL(
SE.AULA,
(
NVL(
AS_COURSE.CLASSROOM_ID,
AS_PROGRAM.CLASSROOM_ID
)
)
)
) CALCULATED_CLASSROOM,
(
SELECT
LISTAGG(MTP.TEXT_CODE, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_CODE ASC
)
FROM
FRM.CES_SESSION_SERVICES_CLASSROOMS P
INNER JOIN FRM.MT_CLASSROOM MTP
ON
(MTP.ID = P.CLASSROOM_ID)
WHERE
P.SERVICE_ID = SERVICES_TABLE.ID
) SERVICE_CLASSROOMS,
SE.HORAINICIO +(1 / 1440 * SE.MINUTOS) SESSION_END_DATE,
AS_PROGRAM.STATUS_ID PROGRAM_STATUS_ID,
AS_PROGRAM.NAME PROGRAM_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_URI,
CASE WHEN EXISTS(
SELECT
CES_MATERIALCOORD.ID
FROM
FRM.CES_MATERIALCOORD
WHERE
CES_MATERIALCOORD.SESSIONSERVICE_ID = SERVICES_TABLE.ID
) THEN '1' ELSE '0'
END AS HASMATERIAL,
SERVICES_TABLE.PENDING_SEND
FROM
FRM.SESIONCURSO SE
INNER JOIN FRM.AS_COURSE ON
(
SE.CURSO = AS_COURSE.ID AND AS_COURSE.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD ON
(
AS_COURSE.PERIOD_ID = AS_PERIOD.ID AND AS_PERIOD.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM ON
(
AS_PERIOD.PROGRAM_ID = AS_PROGRAM.ID AND AS_PROGRAM.REMOVED = '0'
)
INNER JOIN FRM.CES_SESSION_SERVICES SERVICES_TABLE
ON
(
SERVICES_TABLE.SESSION_ID = SE.ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_3 ON
(
MT_CES_SERVICE_LEVEL_3.ID = SERVICES_TABLE.SERVICE_ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_2 ON
(
MT_CES_SERVICE_LEVEL_2.ID = MT_CES_SERVICE_LEVEL_3.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_1 ON
(
MT_CES_SERVICE_LEVEL_1.ID = MT_CES_SERVICE_LEVEL_2.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_STATUS ON
(
MT_CES_STATUS.ID = SERVICES_TABLE.STATUS
)
LEFT JOIN FRM.MT_CES_PROVIDER ON
(
MT_CES_PROVIDER.ID = MT_CES_SERVICE_LEVEL_3.PROVIDER_ID
)
WHERE
AS_PROGRAM.ID = 'TEST'
UNION ALL
SELECT
SERVICES_TABLE.SERVICE_ID SESSION_SERVICE_CATALOG_ID,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_3_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_3_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_2_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_2_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.ID MT_CES_SERVICE_LEVEL_2_ID,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_1_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_1_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_1.ID MT_CES_SERVICE_LEVEL_1_ID,
MT_CES_SERVICE_LEVEL_3.ALARM_DAYS CES_CATALOG_ALARM_DAYS,
MT_CES_SERVICE_LEVEL_3.ADVANCE_DAYS CES_CATALOG_ADVANCE_DAYS,
MT_CES_SERVICE_LEVEL_3.COST_BY_STUDENT CES_CATALOG_COST_BY_STUDENT,
MT_CES_SERVICE_LEVEL_3.DESCRIPTION,
SERVICES_TABLE.COST SESSION_SERVICE_COST,
SERVICES_TABLE.NUM_ITEMS SESSION_SERVICE_NUM_ITEMS,
SERVICES_TABLE.COMMENTS SESSION_SERVICE_COMMENTS,
SERVICES_TABLE.STATUS PROGRAM_SERVICE_STATUS,
MT_CES_STATUS.TEXT_NAME_EN PROGRAM_SERVICE_STATUS_LITERAL,
(
SELECT
LISTAGG(MTP.TEXT_NAME_EN, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_NAME_EN ASC
)
FROM
FRM.MT_CES_SERVICE_LEVEL_3_PROVIDERS P
INNER JOIN FRM.MT_CES_PROVIDER MTP
ON
(MTP.ID = P.PROVIDER_ID)
WHERE
P.MT_CES_SERVICE_LEVEL_3_ID = MT_CES_SERVICE_LEVEL_3.ID
) PROVIDER_LITERAL,
(
SELECT
LISTAGG(PRO.NAME, ', ') WITHIN GROUP(
ORDER BY
PRO.NAME ASC
)
FROM
FRM.SESIONESPECIAL SES
INNER JOIN FRM.AS_PROGRAM PRO
ON
(
SES.IDPROGRAMA = PRO.ID AND PRO.REMOVED = '0'
)
WHERE
PRO.ID != AS_PROGRAM.ID AND SES.HORAINICIO = SE.HORAINICIO AND(
NVL(
SE.AULA,
(AS_PROGRAM.CLASSROOM_ID)
)
) = NVL(SES.AULA, PRO.CLASSROOM_ID)
) PROGRAMS_NAME,
AS_PROGRAM.PARTICIPANTS PROGRAM_PARTICIPANTS,
SE.HORAINICIO SESSION_DATE,
SE.ID SESSION_ID,
'1' IS_SPECIAL_SESSION,
SE.NOMBRE SESSION_TITLE,
SERVICES_TABLE.ID SERVICE_ID,
SERVICES_TABLE.PARTICIPANTS,
SERVICES_TABLE.CLASSROOM_ID SERVICE_CLASSROOM_ID,
SERVICES_TABLE.SERVICE_START,
SERVICES_TABLE.SERVICE_END,
(
SELECT
AUL.TEXT_CODE
FROM
FRM.MT_CLASSROOM AUL
WHERE
AUL.ID = NVL(
SE.AULA,
AS_PROGRAM.CLASSROOM_ID
)
) CALCULATED_CLASSROOM,
(
SELECT
LISTAGG(MTP.TEXT_CODE, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_CODE ASC
)
FROM
FRM.CES_SPECIAL_SESSION_SERVICES_CLASSROOMS P
INNER JOIN FRM.MT_CLASSROOM MTP
ON
(MTP.ID = P.CLASSROOM_ID)
WHERE
P.SERVICE_ID = SERVICES_TABLE.ID
) SERVICE_CLASSROOMS,
SE.HORAINICIO +(1 / 1440 * SE.MINUTOS) SESSION_END_DATE,
AS_PROGRAM.STATUS_ID PROGRAM_STATUS_ID,
AS_PROGRAM.NAME PROGRAM_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_URI,
CASE WHEN EXISTS(
SELECT
CES_MATERIALCOORD.ID
FROM
FRM.CES_MATERIALCOORD
WHERE
CES_MATERIALCOORD.SPECIALSESSIONSERVICE_ID = SERVICES_TABLE.ID
) THEN '1' ELSE '0'
END AS HASMATERIAL,
SERVICES_TABLE.PENDING_SEND
FROM
FRM.SESIONESPECIAL SE
INNER JOIN FRM.AS_PROGRAM ON
(
SE.IDPROGRAMA = AS_PROGRAM.ID AND AS_PROGRAM.REMOVED = '0'
)
INNER JOIN FRM.CES_SPECIAL_SESSION_SERVICES SERVICES_TABLE
ON
(
SERVICES_TABLE.SESSION_ID = SE.ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_3 ON
(
MT_CES_SERVICE_LEVEL_3.ID = SERVICES_TABLE.SERVICE_ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_2 ON
(
MT_CES_SERVICE_LEVEL_2.ID = MT_CES_SERVICE_LEVEL_3.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_1 ON
(
MT_CES_SERVICE_LEVEL_1.ID = MT_CES_SERVICE_LEVEL_2.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_STATUS ON
(
MT_CES_STATUS.ID = SERVICES_TABLE.STATUS
)
LEFT JOIN FRM.MT_CES_PROVIDER ON
(
MT_CES_PROVIDER.ID = MT_CES_SERVICE_LEVEL_3.PROVIDER_ID
)
WHERE
AS_PROGRAM.ID = 'TEST'
ORDER BY
SERVICE_START
DESC
,
SESSION_DATE
DESC
,
MT_CES_SERVICE_LEVEL_1_TEXT_NAME_EN
DESC
UNION ALL
SELECT
SERVICES_TABLE.SERVICE_ID SESSION_SERVICE_CATALOG_ID,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_3_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_3.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_3_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_2_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_2.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_2_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_2.ID MT_CES_SERVICE_LEVEL_2_ID,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_ES MT_CES_SERVICE_LEVEL_1_TEXT_NAME_ES,
MT_CES_SERVICE_LEVEL_1.TEXT_NAME_EN MT_CES_SERVICE_LEVEL_1_TEXT_NAME_EN,
MT_CES_SERVICE_LEVEL_1.ID MT_CES_SERVICE_LEVEL_1_ID,
MT_CES_SERVICE_LEVEL_3.ALARM_DAYS CES_CATALOG_ALARM_DAYS,
MT_CES_SERVICE_LEVEL_3.ADVANCE_DAYS CES_CATALOG_ADVANCE_DAYS,
MT_CES_SERVICE_LEVEL_3.COST_BY_STUDENT CES_CATALOG_COST_BY_STUDENT,
MT_CES_SERVICE_LEVEL_3.DESCRIPTION,
SERVICES_TABLE.COST SESSION_SERVICE_COST,
SERVICES_TABLE.NUM_ITEMS SESSION_SERVICE_NUM_ITEMS,
SERVICES_TABLE.COMMENTS SESSION_SERVICE_COMMENTS,
SERVICES_TABLE.STATUS PROGRAM_SERVICE_STATUS,
MT_CES_STATUS.TEXT_NAME_EN PROGRAM_SERVICE_STATUS_LITERAL,
(
SELECT
LISTAGG(MTP.TEXT_NAME_EN, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_NAME_EN ASC
)
FROM
FRM.MT_CES_SERVICE_LEVEL_3_PROVIDERS P
INNER JOIN FRM.MT_CES_PROVIDER MTP
ON
(MTP.ID = P.PROVIDER_ID)
WHERE
P.MT_CES_SERVICE_LEVEL_3_ID = MT_CES_SERVICE_LEVEL_3.ID
) PROVIDER_LITERAL,
(
SELECT
LISTAGG(PRO.NAME, ', ') WITHIN GROUP(
ORDER BY
PRO.NAME ASC
)
FROM
FRM.SESIONCURSO SES
INNER JOIN FRM.AS_COURSE COU
ON
(
SES.CURSO = COU.ID AND COU.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD PE
ON
(
COU.PERIOD_ID = PE.ID AND PE.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM PRO
ON
(
PE.PROGRAM_ID = PRO.ID AND PRO.REMOVED = '0'
)
WHERE
PRO.ID != AS_PROGRAM.ID AND SES.HORAINICIO = SE.HORAINICIO AND(
NVL(
SE.AULA,
(
NVL(
AS_COURSE.CLASSROOM_ID,
AS_PROGRAM.CLASSROOM_ID
)
)
)
) =(
NVL(
SES.AULA,
(
NVL(
COU.CLASSROOM_ID,
PRO.CLASSROOM_ID
)
)
)
)
) PROGRAMS_NAME,
AS_PROGRAM.PARTICIPANTS PROGRAM_PARTICIPANTS,
SE.HORAINICIO SESSION_DATE,
SE.ID SESSION_ID,
'0' IS_SPECIAL_SESSION,
SE.NOMBRE SESSION_TITLE,
SERVICES_TABLE.ID SERVICE_ID,
SERVICES_TABLE.PARTICIPANTS,
SERVICES_TABLE.CLASSROOM_ID SERVICE_CLASSROOM_ID,
SERVICES_TABLE.SERVICE_START,
SERVICES_TABLE.SERVICE_END,
(
SELECT
AUL.TEXT_CODE
FROM
FRM.MT_CLASSROOM AUL
WHERE
AUL.ID = NVL(
SE.AULA,
(
NVL(
AS_COURSE.CLASSROOM_ID,
AS_PROGRAM.CLASSROOM_ID
)
)
)
) CALCULATED_CLASSROOM,
(
SELECT
LISTAGG(MTP.TEXT_CODE, ', ') WITHIN GROUP(
ORDER BY
MTP.TEXT_CODE ASC
)
FROM
FRM.CES_SESSION_SERVICES_CLASSROOMS P
INNER JOIN FRM.MT_CLASSROOM MTP
ON
(MTP.ID = P.CLASSROOM_ID)
WHERE
P.SERVICE_ID = SERVICES_TABLE.ID
) SERVICE_CLASSROOMS,
SE.HORAINICIO +(1 / 1440 * SE.MINUTOS) SESSION_END_DATE,
AS_PROGRAM.STATUS_ID PROGRAM_STATUS_ID,
AS_PROGRAM.NAME PROGRAM_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_NAME,
MT_CES_SERVICE_LEVEL_3.EXT_LINK_URI,
CASE WHEN EXISTS(
SELECT
CES_MATERIALCOORD.ID
FROM
FRM.CES_MATERIALCOORD
WHERE
CES_MATERIALCOORD.SESSIONSERVICE_ID = SERVICES_TABLE.ID
) THEN '1' ELSE '0'
END AS HASMATERIAL,
SERVICES_TABLE.PENDING_SEND
FROM
FRM.SESIONCURSO SE
INNER JOIN FRM.AS_COURSE ON
(
SE.CURSO = AS_COURSE.ID AND AS_COURSE.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD ON
(
AS_COURSE.PERIOD_ID = AS_PERIOD.ID AND AS_PERIOD.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM ON
(
AS_PERIOD.PROGRAM_ID = AS_PROGRAM.ID AND AS_PROGRAM.REMOVED = '0'
)
INNER JOIN FRM.CES_SESSION_SERVICES SERVICES_TABLE
ON
(
SERVICES_TABLE.SESSION_ID = SE.ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_3 ON
(
MT_CES_SERVICE_LEVEL_3.ID = SERVICES_TABLE.SERVICE_ID
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_2 ON
(
MT_CES_SERVICE_LEVEL_2.ID = MT_CES_SERVICE_LEVEL_3.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_SERVICE_LEVEL_1 ON
(
MT_CES_SERVICE_LEVEL_1.ID = MT_CES_SERVICE_LEVEL_2.PARENT_SERVICE
)
INNER JOIN FRM.MT_CES_STATUS ON
(
MT_CES_STATUS.ID = SERVICES_TABLE.STATUS
)
LEFT JOIN FRM.MT_CES_PROVIDER ON
(
MT_CES_PROVIDER.ID = MT_CES_SERVICE_LEVEL_3.PROVIDER_ID
)
WHERE
SE.ID IN(
SELECT
SES.ID
FROM
FRM.SESIONCURSO SES
INNER JOIN FRM.AS_COURSE COU
ON
(
SES.CURSO = COU.ID AND COU.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD PE
ON
(
COU.PERIOD_ID = PE.ID AND PE.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM PRO
ON
(
PE.PROGRAM_ID = PRO.ID AND PRO.REMOVED = '0'
)
WHERE
SES.HORAINICIO =(
SELECT
SES2.HORAINICIO
FROM
FRM.SESIONCURSO SES2
WHERE
SES2.ID = 'TEST'
) AND(
NVL(
SES.AULA,
(
NVL(
COU.CLASSROOM_ID,
PRO.CLASSROOM_ID
)
)
) =(
SELECT
NVL(
SES2.AULA,
(
NVL(
COU2.CLASSROOM_ID,
PRO2.CLASSROOM_ID
)
)
)
FROM
FRM.SESIONCURSO SES2
INNER JOIN FRM.AS_COURSE COU2
ON
(
SES2.CURSO = COU2.ID AND COU2.REMOVED = '0'
)
INNER JOIN FRM.AS_PERIOD PE2
ON
(
COU2.PERIOD_ID = PE2.ID AND PE2.REMOVED = '0'
)
INNER JOIN FRM.AS_PROGRAM PRO2
ON
(
PE2.PROGRAM_ID = PRO2.ID AND PRO2.REMOVED = '0'
)
WHERE
SES2.ID = 'TEST'
)
)
)
) SUBQUERY
WHERE
ROWNUM <= 1000
)
WHERE
RNUM > 0;
It is about the ORDER BY clause; you can't sort each select statement separately, but the whole result set.
No:
select * from
(select first_query
from some_table
order by whatever --> this can't be here
UNION ALL
select second_query
from another_table
)
where some_condition;
Yes #1:
select * from
(select first_query
from some_table
UNION ALL
select second_query
from another_table
)
where some_condition
order by whatever; --> should be here
Yes #2:
select * from
(select first_query
from some_table
UNION ALL
select second_query
from another_table
order by whatever --> should be here
)
where some_condition
Related
simplify simple query sql
Can someone help me simplify the following query into one line . SELECT LL_ID, LL_VOORNAAM, LB_VAN_VAN, LB_VAN_TOT, LB_VAN_ID, LB_VAN_INUIT_FK, LB_NAAR_INUIT_FK, iu_vorigeinuit_fk, iu_id, LB_NAAR_ID, LB_NAAR_VAN, LB_NAAR_TOT FROM LEERLING INNER JOIN INUIT ON (LL_ID = IU_LEERLING_FK) INNER JOIN ( SELECT LB_ID AS LB_VAN_ID, LB_VAN AS LB_VAN_VAN, LB_TOT AS LB_VAN_TOT, LB_INUIT_FK AS LB_VAN_INUIT_FK FROM LOOPBAAN ) AS LOOPBAAN_VAN ON ( (IU_ID = LB_VAN_INUIT_FK) ) INNER JOIN ( SELECT LB_ID AS LB_NAAR_ID, LB_VAN AS LB_NAAR_VAN, LB_TOT AS LB_NAAR_TOT, LB_INUIT_FK AS LB_NAAR_INUIT_FK FROM LOOPBAAN ) AS LOOPBAAN_NAAR ON ( (iu_id = LB_NAAR_INUIT_FK) ) WHERE (LB_VAN_ID <> LB_NAAR_ID) AND ( LB_NAAR_VAN = DATEADD(1 DAY TO LB_VAN_TOT) ) and LL_ID in (41366) order by ll_naam Currently, I'm getting three repeated values which I want to merge into one. See the result I would like to have. note : "Van" means From and "naar" To
This did it for me... ) AS LOOPBAAN_VAN ON ((IU_VORIGEINUIT_FK = LB_VAN_INUIT_FK) ) SELECT LL_ID, LL_VOORNAAM, LB_VAN_VAN, LB_VAN_TOT, LB_VAN_ID, LB_VAN_INUIT_FK, LB_NAAR_INUIT_FK, iu_vorigeinuit_fk, iu_id, LB_NAAR_ID, LB_NAAR_VAN, LB_NAAR_TOT FROM LEERLING INNER JOIN INUIT ON (LL_ID = IU_LEERLING_FK) INNER JOIN ( SELECT LB_ID AS LB_VAN_ID, LB_VAN AS LB_VAN_VAN, LB_TOT AS LB_VAN_TOT, LB_INUIT_FK AS LB_VAN_INUIT_FK FROM LOOPBAAN ) AS LOOPBAAN_VAN ON ((IU_VORIGEINUIT_FK = LB_VAN_INUIT_FK) ) INNER JOIN ( SELECT LB_ID AS LB_NAAR_ID, LB_VAN AS LB_NAAR_VAN, LB_TOT AS LB_NAAR_TOT, LB_INUIT_FK AS LB_NAAR_INUIT_FK FROM LOOPBAAN ) AS LOOPBAAN_NAAR ON (( iu_id = LB_NAAR_INUIT_FK)) WHERE (LB_VAN_ID <> LB_NAAR_ID) AND ( LB_NAAR_VAN = DATEADD(1 DAY TO LB_VAN_TOT) ) and LL_ID in (41366) order by ll_naam
I am unable to get last date that is max(reservation_date) from below query.Also query is taking much time
My below query is taking long time.I don't know where I am doing wrong.Desire output Resort_name Resource_name last_date("it's booked in the reservation") Arrival date of that reservation SELECT max(rsv.reservation_date), r.name AS resort_name, rsv.name AS resource_name, rsv.reservation_date, rv.arrival_date FROM resorti18n r JOIN resourcebasei18n re ON ( r.resort_id = re.resort_id ) JOIN reservedresourcebase rsv ON (rsv.resource_id=re.resource_id) JOIN reservation rv ON ( rv.resort_id = re.resort_id ) JOIN admin_organisation ao ON ( ao.admin_organisation_id = r.admin_organisation_id ) WHERE rsv.type = 'producttype' group by r.name, rsv.name, rsv.reservation_date, rv.arrival_date;
use row_number() with cte as(SELECT rsv.reservation_date, row_number() over(partition by r.name,rsv.name order by rsv.reservation_date desc) rn, r.name AS resort_name, rsv.name AS resource_name, rsv.reservation_date, rv.arrival_date FROM resorti18n r JOIN resourcebasei18n re ON ( r.resort_id = re.resort_id ) JOIN reservedresourcebase rsv ON (rsv.resource_id=re.resource_id) JOIN reservation rv ON ( rv.resort_id = re.resort_id ) JOIN admin_organisation ao ON ( ao.admin_organisation_id = r.admin_organisation_id ) WHERE rsv.type = 'producttype' ) select * from cte where rn=1 usign subquery select * from (SELECT row_number() over(partition by r.name,rsv.name order by rsv.reservation_date desc) rn, r.name AS resort_name, rsv.name AS resource_name, rsv.reservation_date, rv.arrival_date FROM resorti18n r JOIN resourcebasei18n re ON ( r.resort_id = re.resort_id ) JOIN reservedresourcebase rsv ON (rsv.resource_id=re.resource_id) JOIN reservation rv ON ( rv.resort_id = re.resort_id ) JOIN admin_organisation ao ON ( ao.admin_organisation_id = r.admin_organisation_id ) WHERE rsv.type = 'producttype' ) a where a.rn=1
Why is Oracle REPLACE function not working for this string?
We have a pattern we use all the time and this is usually pretty straightforward. sortOrder IN VARCHAR2 := 'Title' query VARCHAR2(32767) := q'[ SELECT Columns FROM tables ORDER BY {sortOrder} ]'; query := REPLACE(query, '{sortOrder}', sortOrder); But for this string it is not working: WITH appl_List AS ( SELECT DISTINCT appls.admin_phs_ORG_code || TO_CHAR(appls.serial_num, 'FM000000') AS core_proj_number, appls.Appl_ID FROM TABLE(:portfolioTable) appls ), g1SupportingProjCount AS ( SELECT gen1grants.silverchair_id, COUNT(DISTINCT al.Appl_ID) AS ApplCount FROM appl_List al JOIN cg_cited_reference_gen1_grant gen1grants ON al.core_proj_number = gen1grants.ic_serial_num JOIN cg_cited_reference_gen1 gen1refs ON gen1grants.silverchair_id = gen1refs.silverchair_id GROUP BY gen1grants.Silverchair_id ), g1SupportedPubCount AS ( SELECT gen1grants.silverchair_id, COUNT(DISTINCT gen1refs.gen1_wos_uid) AS PubCount FROM appl_List al JOIN cg_cited_reference_gen1_grant gen1grants ON al.core_proj_number = gen1grants.ic_serial_num JOIN cg_cited_reference_gen1 gen1refs ON gen1grants.silverchair_id = gen1refs.silverchair_id GROUP BY gen1grants.Silverchair_id ), g2SupportingProjCount AS ( SELECT gen2grants.silverchair_id, COUNT(DISTINCT al.Appl_ID) AS ApplCount FROM appl_List al JOIN cg_cited_reference_gen2_grant gen2grants ON al.core_proj_number = gen2grants.ic_serial_num JOIN cg_cited_reference_gen2 gen2refs ON gen2grants.silverchair_id = gen2refs.silverchair_id GROUP BY gen2grants.Silverchair_id ), g2SupportedPubCount AS ( SELECT gen2grants.silverchair_id, COUNT(DISTINCT gen2refs.gen2_wos_uid) AS PubCount FROM appl_List al JOIN cg_cited_reference_gen2_grant gen2grants ON al.core_proj_number = gen2grants.ic_serial_num JOIN cg_cited_reference_gen2 gen2refs ON gen2grants.silverchair_id = gen2refs.silverchair_id GROUP BY gen2grants.Silverchair_id ), portfolio_cg_ids AS ( SELECT DISTINCT md.silverchair_id FROM ( SELECT silverchair_id FROM cg_cited_reference_gen1_grant gen1Grants JOIN Appl_List appls ON appls.core_proj_number = gen1Grants.ic_serial_num UNION SELECT silverchair_id FROM cg_cited_reference_gen2_grant gen2Grants JOIN Appl_List appls ON appls.core_proj_number = gen2Grants.ic_serial_num ) grant_sc_ids JOIN cg_metadata md ON grant_sc_ids.silverchair_id = md.silverchair_id ) SELECT silverchairId, TITLE, PMID, PMCID, publication_year as year, referenceCount1Gen, supportingProjectCount1Gen, supportedPublicationCount1Gen, referenceCount2Gen, supportingProjectCount2Gen, supportedPublicationCount2Gen, COUNT(1) OVER() as TotalCount FROM ( SELECT md.SILVERCHAIR_ID silverchairId, md.TITLE, md.PMID, md.PMCID , md.publication_year as year, g1RefCounts.referenceCount1Gen as referenceCount1Gen, g1SupportingProjCount.ApplCount as supportingProjectCount1Gen, g1SupportedPubCount.PubCount as supportedPublicationCount1Gen, g2RefCounts.referenceCount2Gen as referenceCount2Gen, g2SupportingProjCount.ApplCount as supportingProjectCount2Gen, g2SupportedPubCount.PubCount as supportedPublicationCount2Gen, --COUNT(1) OVER() as TotalCount FROM cg_metadata md -- BEGIN datascope to current portfolio JOIN portfolio_cg_ids ON portfolio_cg_ids.silverchair_id = md.silverchair_id -- END datascope to current portfolio LEFT JOIN g1SupportingProjCount ON g1SupportingProjCount.Silverchair_id = md.silverchair_id LEFT JOIN g2SupportingProjCount ON g2SupportingProjCount.Silverchair_id = md.silverchair_id LEFT JOIN g1SupportedPubCount ON g1SupportedPubCount.Silverchair_id = md.silverchair_id LEFT JOIN g2SupportedPubCount ON g2SupportedPubCount.Silverchair_id = md.silverchair_id OUTER APPLY ( Select Count(*) as referenceCount1Gen FROM cg_cited_reference_gen1 g1Refs WHERE g1Refs.silverchair_id = md.silverchair_id ) g1RefCounts OUTER APPLY ( Select Count(*) as referenceCount2Gen FROM cg_cited_reference_gen2 g2Refs WHERE g2Refs.silverchair_id = md.silverchair_id ) g2RefCounts ) results ORDER BY {sortOrder} Are there cases where some kind of special char in the string can keep this from working? I'm kind of perplexed. I've been using this pattern for like 3 years and I've never had this not work. What could be breaking this?
The query has 4000+ characters. The text is probably being truncated somewhere down the line.
SQL Server - get rid of derived query in join
I'm interested in some suggestions with examples on how to 1)get rid of the derived query in the left outer join and 2)remove/consolidate the cte cteSecType in the code below The derived query in the join chooses the minimum assistant_personnel_number out of a list; there can be multiple primary and backup assistants associated with an employee . thx WITH cteAssistants ( executive_personnel_number, assistant_personnel_number, assistant_type, assign_role ) AS ( SELECT CASE WHEN ISNUMERIC(JP.XA_asgn_emplid) = 1 THEN JP.XA_asgn_emplid ELSE '' END AS executive_personnel_number , JAP.emplid AS assistant_personnel_number , LAT1.FIELDVALUE AS assistant_type , LAT3.xlatshortname AS assign_role FROM dbo.XA_ASGN_PRNT AS KAP LEFT OUTER JOIN dbo.XA_ASSIGNMENTS AS KA ON JP.emplid = JAP.emplid AND JP.effdt = JAP.effdt LEFT OUTER JOIN dbo.XA_EMPLOYEES AS EXECT ON EXECT.EMPLID = JP.XA_ASGN_EMPLID LEFT OUTER JOIN dbo.XA_EMPLOYEES AS ASST ON ASST.EMPLID = JAP.EMPLID LEFT OUTER JOIN dbo.XLATITEM AS XLAT1 ON LAT1.fieldname = 'XA_ASGN_TYPE' AND LAT1.fieldvalue = JAP.XA_asgn_type LEFT OUTER JOIN dbo.XLATITEM AS XLAT3 ON LAT3.fieldname = 'XA_ASGN_ROLE' AND LAT3.fieldvalue = JP.XA_asgn_role WHERE JAP.effdt = ( SELECT MAX(effdt) FROM dbo.XA_ASGN_PRNT WHERE emplid = JAP.emplid AND effdt <= GETDATE() ) --Return data only when both executive and assistant are still active; null is for Floaters AND ( EXECT.HR_STATUS = 'A' OR EXECT.HR_STATUS IS NULL ) AND ASST.HR_STATUS = 'A' AND ( JAP.XA_asgn_type = 'F' OR ( JAP.XA_asgn_type IN ( 'A', 'AF' ) AND JP.XA_asgn_person = 'Y' ) ) ), cteSecType ( assistant_personnel_number, SecType ) AS ( SELECT assistant_personnel_number , assistant_type AS SecType FROM cteAssistants GROUP BY assistant_type , assistant_personnel_number ) SELECT EMP.XA_NETWORK_ID AS network_id , EMP.XA_EMPLID AS empid , EMP.XA_EMPLID AS employeeNumber , EMP.XA_FIRST_NAME AS first_name , EMP.XA_LAST_NAME AS last_name , SECRES.SecType AS SecType , AsstInfo.XA_fml_pref_name AS PrimaryAssistant_FML , AsstInfo.XA_network_id AS PrimaryAssistant_Newtork_ID , AsstInfo.XA_EMPLID AS PrimaryAssistant_EmpID , AsstInfo.XA_phone_ext AS PrimaryAssitant_Extension FROM dbo.XA_EMPLOYEES AS EMP LEFT OUTER JOIN dbo.XA_EMPLOYEES AS MGR ON EMP.supervisor_id = MGR.emplid LEFT OUTER JOIN cteSecType AS SECRES ON CAST(CAST(SECRES.assistant_personnel_number AS INT) AS VARCHAR(11)) = EMP.XA_EMPLID LEFT OUTER JOIN ( SELECT executive_personnel_number AS AttorneyID , MIN(assistant_personnel_number) AS AssistantID FROM cteEmployeeAssistants AS A WHERE ( assign_role = 'Primary' ) GROUP BY executive_personnel_number ) AS ASST ON ASST.AttorneyID = EMP.XA_EMPLID LEFT OUTER JOIN dbo.XA_EMPLOYEES AS ASSTINFO ON ASSTINFO.XA_EMPLID = ASST.AssistantID WHERE ( EMP.HR_STATUS = 'A' ) AND ( EMP.PER_ORG IN ( 'EMP', 'CWR' ) ) AND ( ISNULL(EMP.XA_NETWORK_ID, '') <> '' ) GO
Join with 2 temporary table
How to create one more temporary table in below query to have join with the same. WITH financial_move_out_due_days AS ( SELECT property_id, management_company_id, value FROM property_preferences WHERE property_id IN ( 112,332 ) AND management_company_id = 23 ) SELECT l.id, l.primary_customer_id, ( c.name_first || c.name_last ) AS customer_name_full, c.email_address AS customer_email_address, c.phone_number AS phone_number FROM leases l JOIN customers c ON ( c.management_company_id = l.management_company_id AND c.id = l.primary_customer_id ) JOIN financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id ) WHERE l.management_company_id = 23 AND l.property_unit_id IS NOT NULL AND l.unit_space_id IS NOT NULL';
Try like this: WITH financial_move_out_due_days AS ( SELECT property_id, management_company_id, value FROM property_preferences WHERE property_id IN ( 112,332 ) AND management_company_id = 23 ), another_financial_move_out_due_days AS ( SELECT property_id, management_company_id, value FROM property_preferences WHERE property_id IN ( 112,332 ) AND management_company_id = 23 ) SELECT l.id, l.primary_customer_id, ( c.name_first || c.name_last ) AS customer_name_full, c.email_address AS customer_email_address, c.phone_number AS phone_number FROM leases l JOIN customers c ON ( c.management_company_id = l.management_company_id AND c.id = l.primary_customer_id ) JOIN financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id ) JOIN another_financial_move_out_due_days fmpodd ON ( fmpodd.management_company_id = l.management_company_id AND fmpodd.property_id = l.property_id ) WHERE l.management_company_id = 23 AND l.property_unit_id IS NOT NULL AND l.unit_space_id IS NOT NULL';
WITH FirstTable AS ( ), SecondTable AS ( ), ....