如何使用plsql或sql将' - '记录与'non--'记录合并?

时间:2015-12-04 15:55:57

标签: sql oracle plsql oracle-sqldeveloper plsqldeveloper

Please see this image

ID SEQNO DATE TEXT

CD1 1   12/1/2015   HELLO HO    
CD1 2   12/1/2015    --W ARE YOU    
CD1 3   12/1/2015    --? received an account recovery   
CD1 4   12/1/2015    --request on   
CD1 5   12/1/2015    -- Stack   
CD1 6   12/1/2015    -- Overflow.   
CD1 7   12/2/2015   This email address is   
CD1 8   12/2/2015    ---associated with 
CD1 9   12/2/2015    --an account, but  
CD1 10  12/2/2015    ---no password is associated.  
CD1 11  12/3/2015   As a reminder, you  
CD1 12  12/3/2015    --can use any of the following credentials.    
CD1 13  12/3/2015   once logged in, you can review existing credentials.    
CD1 14  12/3/2015   If you did not initiate this account recovery reque 
CD1 15  12/3/2015    --st, just ignore this email   
Required Answer:                
ID  SEQNO   DATE    TEXT    
CD1 1   12/1/2015   HELLO HOW ARE YOU.? received an account recovery request on StackOverflow.  
CD1 7   12/2/2015   This email address is associated with an account, but no password is associated.    
CD1 11  12/3/2015   As a reminder, you can use any of the following credentials.    
CD1 13  12/3/2015   once logged in, you can review existing credentials.    
CD1 14  12/3/2015   If you did not initiate this account recovery request, just ignore this email   

代码?你能帮助我用plsql或sql将' - '记录与'non--'记录合并吗?

1 个答案:

答案 0 :(得分:1)

如何做到这一点并不明显。您需要识别组。一种方法是为每行分配其前面的非--行数。您可以使用累计金额来完成此操作。这提供了聚合所需的信息:

select id, min(seqno) as seqno, min(date) as date,
       listagg((case when text like '--' then substr(text, 3)
                     else text
                end), ' ') within group (order by seqno)
from (select t.*,
             sum(case when text not like '--%' then 1 else 0 end) over
                 (order by id, seqno) as grp
      from t
     ) t
group by grp, id;