[MSSQL] 合併多筆資料至同一欄


<原始資料> 

<SQL執行結果>


select ogb01, ogb04, Left(ogb03List, LEN(ogb03List)-1) as ogb03List
from (
select distinct ogb01, ogb04,
     (
    select cast(ogb03 as nvarchar) + ',' 
    from ogb_file as ogb2
    where ogb2.ogb01='ABC-0001'
      and ogb2.ogb04=ogb1.ogb04
    FOR XML PATH('')
     ) as ogb03List
from ogb_file ogb1
where ogb1.ogb01='ABC-0001'
) A


select distinct ogb01, ogb04,
     substring((
    select ',' + cast(ogb03 as nvarchar) 
    from ogb_file as ogb2
    where ogb2.ogb01='ABC-0001'
      and ogb2.ogb04=ogb1.ogb04
    FOR XML PATH('')
     ),2,1000) as ogb03List
from ogb_file ogb1
where ogb1.ogb01='ABC-0001'





留言

這個網誌中的熱門文章

ORA-12514: TNS: 監聽器目前不知道連線描述區中要求的服務

Oracle 例外控制(Exception Control)

Oracle 工作排程 DBMS_JOB 筆記