ORACLE/SQL: wm_concat & order by

前端 未结 6 927
梦如初夏
梦如初夏 2020-12-19 18:17

I\'m using oracle 11 (not sure about the exact version, but since LISTAGG doesn\'t work, I suppose it\'s not release 2) through ODBC and crystal reports 2008.

Here

6条回答
  •  星月不相逢
    2020-12-19 19:17

    For anyone that is still using wm_CONCAT (a.k.a. older db versions): The solution is to add distinct condition, it will then also apply ascending order to the concatenated values.

    Don't ask why it's not documented, but it will work.

    Also, using a order by in a subquery, previous to wm_concat will just randomize the order, so it shouldn't have been recommended.

    Example for the requested SQL:

    SELECT TASK_CARD, WM_CONCAT(distinct code) as ZONES
    FROM ODB.TASK_CARD_CONTROL
    WHERE ODB.TASK_CARD_CONTROL.CONTROL_CATEGORY = 'ZONE'
    GROUP BY TASK_CARD;
    

    Just be warned that the distinct option does not work when used in procedures/packages .

提交回复
热议问题