ORACLE 多列合并成一行数据 WM_CONCAT函数以及REPLACE

WM_CONCAT()方法 注意字符长度

SELECT BERTHCODE,tpf.freedatetype,

( SELECT  WM_CONCAT(SBPT.PARKSTIME||'~'||SBPT.PARKETIME) SAREE FROM  

SYS_BERTHANDTYPE SBT LEFT JOIN  SYS_BERTHTYPEPARKTIME SBPT  

ON SBT.BERTHTYPEID = SBPT.BERTHTYPEID WHERE BERTHCODE='114163' AND 

 SBPT.DATETYPE = 1 AND  SBPT.ISALLOW = 2)  

 NOTALLOWTIMESPAN, WM_CONCAT(SUBSTR(TO_CHAR(FREESTARTTIME,'YYYY-MM-DD HH24:MI:SS'),11,9)||'~'||SUBSTR(TO_CHAR(FREEENDTIME,'YYYY-MM-DD HH24:MI:SS'),11,9)) 
FREETIMESPAN
FROM TRA_PRICINGBERTH TPB LEFT JOIN TRA_PRICINGFREE TPF ON TPB.PRICINGSTRATEGYID = TPF.PRICINGSTRATEGYID WHERE BERTHCODE='114163' AND FREEDATETYPE=1 GROUP BY BERTHCODE,tpf.freedatetype order by BERTHCODE

 

 可以使用以下这个方法REPLACE(WM_CONCAT())


SELECT TPB.BERTHCODE, ( SELECT REPLACE(WM_CONCAT(SBPT.PARKSTIME||'~'||SBPT.PARKETIME)OVER (PARTITION BY BERTHCODE,SBPT.PARKETIME), ',', ';') AS SAREE FROM SYS_BERTHANDTYPE SBT LEFT JOIN SYS_BERTHTYPEPARKTIME SBPT ON SBT.BERTHTYPEID = SBPT.BERTHTYPEID WHERE SBT.BERTHCODE='114163' AND SBPT.DATETYPE = 1 AND SBPT.ISALLOW =2 ) AS NOTALLOWTIMESPAN ,
REPLACE(WM_CONCAT(SUBSTR(TO_CHAR(FREESTARTTIME,'YYYY-MM-DD HH24:MI:SS'),11,9) ||'~'||SUBSTR(TO_CHAR(FREEENDTIME,'YYYY-MM-DD HH24:MI:SS'),11,9)), ',', ';') AS FREETIMESPAN FROM TRA_PRICINGBERTH TPB LEFT JOIN TRA_PRICINGFREE TPF ON TPB.PRICINGSTRATEGYID = TPF.PRICINGSTRATEGYID WHERE TPB.BERTHCODE='114163' AND TPF.FREEDATETYPE=1 GROUP BY BERTHCODE

如果遇到版本问题 在Oracle11G下可以使用以下方法 LISTAGG()

SELECT  TPB.BERTHCODE, ( SELECT  LISTAGG( (SBPT.PARKSTIME||'~'||SBPT.PARKETIME), '; ' ) WITHIN GROUP (ORDER BY SBPT.PARKTIMEID)

 AS SAREE  FROM SYS_BERTHANDTYPE SBT

LEFT JOIN  SYS_BERTHTYPEPARKTIME SBPT ON SBT.BERTHTYPEID = SBPT.BERTHTYPEID 

      WHERE SBT.BERTHCODE='114163' AND  SBPT.DATETYPE = 1 AND  SBPT.ISALLOW =2

      ) AS NOTALLOWTIMESPAN ,

      LISTAGG(SUBSTR(TO_CHAR(FREESTARTTIME,'YYYY-MM-DD HH24:MI:SS'),11,9)

       ||'~'||SUBSTR(TO_CHAR(FREEENDTIME,'YYYY-MM-DD HH24:MI:SS'),11,9), '; ')WITHIN GROUP (ORDER BY TPF.FREEID)

       AS  FREETIMESPAN       

       FROM TRA_PRICINGBERTH  TPB 

       LEFT JOIN TRA_PRICINGFREE TPF  ON TPB.PRICINGSTRATEGYID = TPF.PRICINGSTRATEGYID 

      WHERE 

     TPB.BERTHCODE='114163'  

    AND TPF.FREEDATETYPE=1  

      GROUP BY BERTHCODE

 

 

 

WM_CONCAT 合并成一行数据列 数据量大会超出字符长度

SELECT wm_concat(sbpt.ParkStime||'~'||sbpt.ParkEtime) FROM SYS_BerthTypeParkTime sbpt

ORACLE 多列合并成一行数据 WM_CONCAT函数以及REPLACE

REPLACE 分成多列
SELECT  REPLACE(sbpt.ParkStime||'~'||sbpt.ParkEtime,',')

 FROM SYS_BerthTypeParkTime sbpt

ORACLE 多列合并成一行数据 WM_CONCAT函数以及REPLACE

你可能感兴趣的:(wm_concat)