sql - DB2 Concatenate行

sql - DB2 Concatenate行,第1张

我正在尝试查询iSeries DB2 v6r1m0。我想借用Concatenate many rows into a single text string?

的答案
DECLARE @Names VARCHAR(8000) 
SELECT @Names = COALESCE(@Names   ', ', '')   Name
FROM People
WHERE Name IS NOT NULL

我试图将它应用于下面的查询,但失败了。我正在尝试连接这些评论。

ATOMIC抛出错误。它取自某处的一个答案。 @comments和评论不起作用。 Section是有效的令牌。不确定这意味着什么。

BEGIN ATOMIC
DECLARE @comments varchar(100)
SELECT
    mh.ID
    ,mh.OtherStuff
    ,me.ID
    ,@Comments = COALESCE(@Comments   '<br />' ,")   me.Comment


FROM
    /*insertTblName*/ mh

INNER JOIN 
    /*insertTblName*/ me
    ON 
        me.ID = mh.ID
WHERE
    me.ID = @parameter
END

我正在努力实现这一目标。

ID      Comment
0       Hello
0       World
1       Foo
1       Bar

要...

ID     Comment
0      Hello World
1      Foo Bar

我经常在System i Navigator中测试我的SQL语句,然后在ADO.Net中使用它们。

最佳答案:

2 个答案:

答案 0 :(得分:1)

尝试使用此示例和数据来了解该过程,如果您解决了该过程,请告诉我。

CREATE TABLE QTEMP/EMP (DEPTNO NUMERIC NOT NULL WITH DEFAULT, ENAME
CHAR ( 10) NOT NULL WITH DEFAULT, EMPNO NUMERIC NOT NULL WITH      
DEFAULT);                                                           


insert into emp values (10,'CLARK ',1),  
                       (10,'KING  ',2),  
                       (10,'MILLER',3),  
                       (20,'SMITH ',4),  
                       (20,'ADAMS ',5),  
                       (20,'FORD  ',6),  
                       (20,'SCOTT ',7),  
                       (20,'JONES ',8),  
                       (30,'ALLEN ',9),  
                       (30,'BLAKE ',10), 
                       (30,'MARTIN',11), 
                       (30,'JAMES ',12), 
                       (30,'TURNER',13), 
                       (30,'WARD  ',14)  

with x (deptno, cnt, list, empno, len)                             
 as (                                                               
select z.deptno, (select count(*) from emp y where y.deptno=z.deptno                                                   group by y.deptno)   
, cast(ename as varchar(100)), empno, 1                             
 from emp z                                                         
 union all                                                          
 select x.deptno, x.cnt, x.list ||' '|| e.ename, e.empno, x.len 1   
 from emp e, x                                                      
 where e.deptno = x.deptno                                          
 and e.empno > x. empno                                             
 )                                                                  
 select deptno, list                                                
 from x                                                             
 where len=cnt

结果就是这样。

DEPTNO   LIST                                                                        
    10   CLARK      KING       MILLER                                                
    20   SMITH      ADAMS      FORD       SCOTT      JONES                           
    30   ALLEN      BLAKE      MARTIN     JAMES      TURNER     WARD                 

答案 1 :(得分:0)

优秀代码,JairoFloresS!它对我来说很漂亮。

我只是略微修改了你的代码以使它更通用:我删除了任意列,而是使用相对记录号来跟踪原始记录。

它仍然有效(至少在V7R1上)!

CREATE TABLE QTEMP/EMP 
(
  DEPTNO NUMERIC    NOT NULL WITH DEFAULT,
  ENAME CHAR ( 10)  NOT NULL WITH DEFAULT
);  


insert into emp values (10,'CLARK '),  
                       (10,'KING  '),  
                       (10,'MILLER'),  
                       (20,'SMITH '),  
                       (20,'ADAMS '),  
                       (20,'FORD  '),  
                       (20,'SCOTT '),  
                       (20,'JONES '),  
                       (30,'ALLEN '),  
                       (30,'BLAKE '), 
                       (30,'MARTIN'), 
                       (30,'JAMES '), 
                       (30,'TURNER'), 
                       (30,'WARD  ')  
;


-- Original data:
select * from qtemp/emp  ;


-- Pivoted and grouped data:
with x (deptno, cnt, list, empno, len) as
 (select z.deptno, 
   (select count(*) from emp y 
    where y.deptno=z.deptno 
    group by y.deptno),
  cast(z.ename as varchar(100)),
  rrn(z), 1  
  from emp z 
 union all  
  select x.deptno,
     x.cnt,
     strip(x.list) ||', '|| e.ename,
     rrn(e),
     x.len 1   
  from emp e, x 
  where e.deptno = x.deptno and rrn(e) > x. empno 
 )  
 select deptno, list, len headcount 
 from x 
 where len=cnt 
;

产生的输出如下:

原始数据:

DEPTNO  ENAME
10      CLARK       
10      KING        
10      MILLER      
20      SMITH       
20      ADAMS       
20      FORD        
20      SCOTT       
20      JONES       
30      ALLEN       
30      BLAKE       
30      MARTIN      
30      JAMES       
30      TURNER      
30      WARD        

透视和分组数据:

DEPTNO  LIST                                    HEADCOUNT
10      CLARK, KING, MILLER                             3   
20      SMITH, ADAMS, FORD, SCOTT, JONES                5       
30      ALLEN, BLAKE, MARTIN, JAMES, TURNER, WARD       6   
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复