使用WITHIN GROUP更新

时间:2016-10-18 19:37:45

标签: oracle group-by listagg

我已经写了以下声明,该声明以我需要更新另一个表的格式返回数据,但是我正在努力应对更新

SELECT element_id,
  LISTAGG(cast(0 as varchar2(20))||', '|| VALUE, ' | ') WITHIN GROUP (ORDER BY display_order)
FROM EDRN.MD$$_ELEMENT_VALUES
WHERE element_id IN
  (SELECT element_id FROM EDRN_NEW.DATA_DICTIONARY)
GROUP BY element_id;

我做了一个基本的转换为UPDATE语句

    UPDATE EDRN_NEW.DATA_DICTIONARY
SET Choices = (LISTAGG(CAST(0 AS VARCHAR2(20))||', '|| VALUE, ' | ') WITHIN GROUP (ORDER BY display_order)
FROM EDRN.MD$$_ELEMENT_VALUES
WHERE element_id IN
  (SELECT element_id FROM EDRN_NEW.DATA_DICTIONARY)
GROUP BY element_id);

这收到了" ORA-00934:这里不允许使用组功能"错误。我不确定如何删除组功能但保留我需要的数据格式?

1 个答案:

答案 0 :(得分:1)

您需要使用子查询listagg()。在这种情况下,相关子查询:

update EDRN_NEW.DATA_DICTIONARY dd
    set choices = (SELECT LISTAGG(cast(0 as varchar2(20))||', '|| VALUE, ' | ') WITHIN GROUP (ORDER BY display_order)
                   FROM EDRN.MD$$_ELEMENT_VALUES ev
                   WHERE ev.element_id = dd.element_id
                  )
    where exists (select 1 
                  from EDRN.MD$$_ELEMENT_VALUES ev
                  where ev.element_id = dd.element_id 
                 );