简体   繁体   中英

Oracle SQL left outer join

I am trying to take two queries and join the reults of query 2 (q2) to query 1 (q1) where usid match on both tables. I am getting the following error:

ORA-00904: "Q2"."ENODEB_ID": invalid identifier.

SELECT 
  q1.Date_Range, q1.usid, q2.enodeb_id
FROM
(SELECT to_char(to_date(PERIODSTART,'DD/MM/YYYY'), 'MM-DD') || '-' || to_char(to_date(PERIODEND,'DD/MM/YYYY'), 'MM-DD') as Date_Range,
      usid FROM CQI_DASH.REGIONAL_LCQI_TOPOFFEND) q1
LEFT JOIN
(select usid, LISTAGG(ENODEB_ID, ',') WITHIN GROUP (ORDER BY ENODEB_ID)
FROM (
select usid,
       ENODEB_ID,
       row_number() over (partition by usid, ENODEB_ID order by usid) as rn
from AXIOM.NRS_LTE_ALU_CELLS
order by usid, ENODEB_ID)
WHERE rn = 1 
GROUP BY usid) q2
ON q1.usid = q2.usid;

The select ed columns relating to q2 are here:

select usid, LISTAGG(ENODEB_ID, ',') WITHIN GROUP (ORDER BY ENODEB_ID)

You don't have a column there named ENODEB_ID , thus the error. Perhaps you meant to alias the listagg ?

select usid, LISTAGG(ENODEB_ID, ',') WITHIN GROUP (ORDER BY ENODEB_ID) as ENODEB_ID

In your outer query you select q2.enodeb_id .

However, in the definition of q2 you only select a LISTAGG, and you don't give it an alias. Did you mean to call it enodeb_id ?

The LISTAGG(ENODEB_ID... needs a column name alias. Is that what you want to be called as ENODEB_ID ?

If yes, try the following. Also if you format your SQL properly, it is easier to read and debug :)

SELECT 
  q1.Date_Range, q1.usid, q2.enodeb_id
FROM ( 
    SELECT to_char(to_date(PERIODSTART,'DD/MM/YYYY'), 'MM-DD') || '-' || to_char(to_date(PERIODEND,'DD/MM/YYYY'), 'MM-DD') as Date_Range,
        usid
    FROM CQI_DASH.REGIONAL_LCQI_TOPOFFEND) q1
    LEFT JOIN ( 
        SELECT usid, 
        LISTAGG(ENODEB_ID, ',') 
        WITHIN GROUP ( ORDER BY ENODEB_ID ) AS ENODEB_ID
        FROM (
        SELECT usid,
           ENODEB_ID,
           row_number() over (partition by usid, ENODEB_ID order by usid) as rn
        FROM AXIOM.NRS_LTE_ALU_CELLS
        ORDER BY usid, ENODEB_ID
        )
WHERE rn = 1 
GROUP BY usid) q2
ON q1.usid = q2.usid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM