日期:2014-05-16  浏览次数:20743 次

存储过程编写问题
场景:
1 4105000000000180 11
2 4105000000000180 41
3 4105000000000180 31
4 4105000000000180 51
5 4105000000000181 42
6 4105000000000181 12
7 4105000000000181 11
8 4105000000000181 4
9 4105000000000188 45
10 4105000000000188 32
11 4105000000000188 21
12 4105000000000189 10

目标:
1 4105000000000180 11 41 31 51
5 4105000000000181 42 12 11 4
9 4105000000000188 45 32 21
12 4105000000000189 10

注:不是拼接,是行转列,数据很多,分组很多,所以需要用存储过程解决,
  11 41 31 51 '' ''
  42 12 11 4 '' ''
  45 32 21 '' '' ''
  10 '' '' '' '' ''
  这个最多六列,没有数据的为空
请高人帮忙解答,谢谢

------解决方案--------------------
ms不需要存储过程,用wm_concat函数就可以实现

create table t(id int, x int, y int);
insert into t values (1, 4105000000000180, 11);
insert into t values (2, 4105000000000180, 41);
insert into t values (3, 4105000000000180, 31);
insert into t values (4, 4105000000000180, 51);
insert into t values (5, 4105000000000181, 42);
insert into t values (6, 4105000000000181, 12);
insert into t values (7, 4105000000000181, 11);
insert into t values (8, 4105000000000181, 4);
insert into t values (9, 4105000000000188, 45);
insert into t values (10, 4105000000000188, 32);
insert into t values (11, 4105000000000188, 21);
insert into t values (12, 4105000000000189, 10);
commit;

select min(id), wm_concat(y) from t group by x;