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
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
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.
I am trying to run below query but I am getting error on MS Access, "Syntax error in Join Operation"
Below is my query -
select
v.City,
v.CURRENCY,
(
select
Sum(VSDEH.NORM_PRICE_MEDIUM*BCIEH.WEIGHT_OR_MULTIPLIER) AS BaseMedium
FROM (
// This line has error ->> ([VSURVEYDATA] AS VSDEH INNER JOIN [BASKET_CONTENT_ITEMS] AS BCIEH ON VSDEH.ITEM = BCIEH.ITEM_ID)
INNER JOIN [EXCHANGE_RATES] AS EXREH on VSDEH.SURVEY_DATE = EXREH.RATE_DATE AND VSDEH.CURRENCY = EXREH.BASE_CURRENCY_ID
)
WHERE (
VSDEH.SURVEY_DATE = v.SURVEY_DATE
AND BCIEH.LINE_OF_BUSINESS_ID='ICOL'
AND BCIEH.BASKET_ID= b.BASKET_ID
AND BCIEH.ITEM_ID not in ( '215','216','326')
AND EXREH.HOST_CURRENCY_ID= ex.HOST_CURRENCY_ID
AND EXREH.RATE_SET_ID=' '
AND VSDEH.CITY in (v.City)
)
GROUP BY VSDEH.CITY, VSDEH.CURRENCY
) as BaseMediumEH
FROM ((
([VSURVEYDATA] AS VSDEH INNER JOIN [BASKET_CONTENT_ITEMS] AS BCIEH ON VSDEH.ITEM = BCIEH.ITEM_ID)
INNER JOIN [EXCHANGE_RATES] AS EXREH on VSDEH.SURVEY_DATE = EXREH.RATE_DATE AND VSDEH.CURRENCY = EXREH.BASE_CURRENCY_ID
)
INNER JOIN qSTAHostCity ON VSURVEYDATA.CITY = qSTAHostCity.HostCity)
WHERE (
v.SURVEY_DATE = [Survey Date]
AND b.LINE_OF_BUSINESS_ID='ICOL'
AND b.BASKET_ID= [Basket ID]
AND ex.HOST_CURRENCY_ID='USD'
AND ex.RATE_SET_ID=' ')
GROUP BY v.CITY, v.CURRENCY ORDER BY v.CITY
Can anyone suggest what am I doing wrong here ?
Thanks
You miss two table alias :
Replace *****TableAlias***** by the table alias name you need to do your join.
select
v.City,
v.CURRENCY,
(
select
Sum(VSDEH.NORM_PRICE_MEDIUM*BCIEH.WEIGHT_OR_MULTIPLIER) AS BaseMedium
FROM (
// This line has error ->> ([VSURVEYDATA] AS VSDEH INNER JOIN [BASKET_CONTENT_ITEMS] AS BCIEH ON VSDEH.ITEM = BCIEH.ITEM_ID) *****TableAlias*****
INNER JOIN [EXCHANGE_RATES] AS EXREH on VSDEH.SURVEY_DATE = EXREH.RATE_DATE AND VSDEH.CURRENCY = EXREH.BASE_CURRENCY_ID
)
WHERE (
VSDEH.SURVEY_DATE = v.SURVEY_DATE
AND BCIEH.LINE_OF_BUSINESS_ID='ICOL'
AND BCIEH.BASKET_ID= b.BASKET_ID
AND BCIEH.ITEM_ID not in ( '215','216','326')
AND EXREH.HOST_CURRENCY_ID= ex.HOST_CURRENCY_ID
AND EXREH.RATE_SET_ID=' '
AND VSDEH.CITY in (v.City)
)
GROUP BY VSDEH.CITY, VSDEH.CURRENCY
) as BaseMediumEH
FROM ((
([VSURVEYDATA] AS VSDEH INNER JOIN [BASKET_CONTENT_ITEMS] AS BCIEH ON VSDEH.ITEM = BCIEH.ITEM_ID) *****TableAlias*****
INNER JOIN [EXCHANGE_RATES] AS EXREH on VSDEH.SURVEY_DATE = EXREH.RATE_DATE AND VSDEH.CURRENCY = EXREH.BASE_CURRENCY_ID
)
INNER JOIN qSTAHostCity ON VSURVEYDATA.CITY = qSTAHostCity.HostCity)
WHERE (
v.SURVEY_DATE = [Survey Date]
AND b.LINE_OF_BUSINESS_ID='ICOL'
AND b.BASKET_ID= [Basket ID]
AND ex.HOST_CURRENCY_ID='USD'
AND ex.RATE_SET_ID=' ')
GROUP BY v.CITY, v.CURRENCY ORDER BY v.CITY