Oracle SQL-ListAgg-ORA-00923:找不到期望的FROM关键字

时间:2019-04-23 11:30:13

标签: sql oracle oracle-bi

我的表格结构如下

PERSON_ID | SUFFIX | Details
-----------------------------
12345     | Mr     | ABC
12345     | Mr     | DEF
12345     | Mr     | GHI

这是我的SQL

SELECT PERSON_ID, SUFFIX, LISTAGG(Details, ' ') WITHIN GROUP (ORDER BY PERSON_ID) AS Deets
FROM TABLE_NAME 
GROUP BY PERSON_ID, SUFFIX

所以我想将多行放入一列中。但是我收到以下错误:

ORA-00923: FROM keyword not found where expected

有什么想法为什么会这样?我的TABLE_NAME是使用WITH AS语句构建的。我可以很好地从TABLE_NAME中提取数据,将数据和所有“正常”内容分组

完整代码

WITH
person_id
AS (
    SELECT DISTINCT person_id
    FROM PER_ALL_PEOPLE_F
),

suffix AS (
SELECT person_id, suffix 
FROM PER_PERSON_NAMES_F 
WHERE
    NAME_TYPE = 'GLOBAL' 
     AND TRUNC(SYSDATE) BETWEEN TRUNC(EFFECTIVE_START_date) AND TRUNC(EFFECTIVE_END_DATE) 
GROUP BY person_id, suffix
)
,
person_id_data
AS (
    SELECT DISTINCT person_id
    FROM PER_ALL_ASSIGNMENTS_M
    WHERE person_id IN (SELECT person_id FROM person_id)
    AND ASSIGNMENT_STATUS_TYPE = 'ACTIVE' 
),

-- GETTING NUMBERS
Contact_Phone_Numbers As (SELECT 
    PERSON_ID, 
    OBJECT_VERSION_NUMBER, 
    PHONE_ID, 
    DATE_FROM, 
    DATE_TO, 
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(PHONE_TYPE,'O','Other'),'HM','Home Mobile'),'HF','Home Fax'),'H3','Third Home'),'H2','Second Home'),'H1','Home'),'WM','Work Mobile'),'WF','Work Fax'),'W3','Third Work'),'W2','Second Work'),'W1','Work') AS PHONE_TYPE, 
    PHONE_NUMBER, 
    SEARCH_PHONE_NUMBER, 
    COUNTRY_CODE_NUMBER 

FROM 
    PER_PHONES 

WHERE 
    PERSON_ID IN (SELECT person_id FROM person_id_data))
,

NEARLY_THERE AS (
    SELECT A.PERSON_ID, A.PHONE_TYPE, A.PHONE_NUMBER, B.SUFFIX
    FROM Contact_Phone_Numbers A
    LEFT JOIN suffix B
    ON A.PERSON_ID = B.PERSON_ID
    ORDER BY A.PERSON_ID
), 

SO_CLOSE AS (
SELECT PERSON_ID, CONCAT(PHONE_TYPE, CONCAT (' - ' , PHONE_NUMBER)) AS THE_DETAILS, SUFFIX
FROM NEARLY_THERE
ORDER BY PERSON_ID
),

FINAL AS (
    SELECT * FROM SO_CLOSE
)

SELECT PERSON_ID, SUFFIX, LISTAGG(THE_DETAILS, ' ') WITHIN GROUP (ORDER BY PERSON_ID) AS Deets
FROM FINAL 
GROUP BY PERSON_ID, SUFFIX

1 个答案:

答案 0 :(得分:0)

(这更多是评论,但实际评论太多/格式)

如果运行非CTE版本会怎样?

SELECT 
  paam.person_id, 
  ppnf.suffix,
  LISTAGG(
    CONCAT(
      DECODE(pp.PHONE_TYPE,'O','Other','HM','Home Mobile','HF','Home Fax','H3','Third Home','H2','Second Home','H1','Home','WM','Work Mobile','WF','Work Fax','W3','Third Work','W2','Second Work','W1','Work'),
      ' - ',
      pp.PHONE_NUMBER
    ),
    ' '
  ) WITHIN GROUP (ORDER BY PERSON_ID) as Deets

FROM 
  PER_ALL_ASSIGNMENTS_M paam
  INNER JOIN 
  PER_ALL_PEOPLE_F papf
  ON paam.person_id = papf.person_id

  INNER JOIN 
  PER_PHONES pp
  ON
    paam.person_id = pp.person_id

  LEFT JOIN 
  PER_PERSON_NAMES_F ppnf
  ON 
    paam.PERSON_ID = ppnf.PERSON_ID AND
    ppnf.NAME_TYPE = 'GLOBAL' AND
    TRUNC(SYSDATE) BETWEEN TRUNC(ppnf.EFFECTIVE_START_date) AND TRUNC(ppnf.EFFECTIVE_END_DATE) 

WHERE
  paam.ASSIGNMENT_STATUS_TYPE = 'ACTIVE' 

GROUP BY PERSON_ID, SUFFIX

注意;由于此查询省略了CTE表单具有的一些DISTINCT操作,因此可能会看到一些较小的语法错误和结果差异。我建议您在没有listagg / group的情况下进行修复,以免出现重复的行(对where子句/联接条件进行排序,以便消除笛卡尔积,而不是稍后将它们区别开来)

即解决此问题:

SELECT 
  paam.person_id, 
  ppnf.suffix,

    CONCAT(
      DECODE(pp.PHONE_TYPE,'O','Other','HM','Home Mobile','HF','Home Fax','H3','Third Home','H2','Second Home','H1','Home','WM','Work Mobile','WF','Work Fax','W3','Third Work','W2','Second Work','W1','Work'),
      ' - ',
      pp.PHONE_NUMBER
    )

FROM 
  PER_ALL_ASSIGNMENTS_M paam
  INNER JOIN 
  PER_ALL_PEOPLE_F papf
  ON paam.person_id = papf.person_id

  INNER JOIN 
  PER_PHONES pp
  ON
    paam.person_id = pp.person_id

  LEFT JOIN 
  PER_PERSON_NAMES_F ppnf
  ON 
    paam.PERSON_ID = ppnf.PERSON_ID AND
    ppnf.NAME_TYPE = 'GLOBAL' AND
    TRUNC(SYSDATE) BETWEEN TRUNC(ppnf.EFFECTIVE_START_date) AND TRUNC(ppnf.EFFECTIVE_END_DATE) 

WHERE
  paam.ASSIGNMENT_STATUS_TYPE = 'ACTIVE' 

然后重新引入LISTAGG