+ -
当前位置:首页 → 问答吧 → 有什么办法给数据集汇总

有什么办法给数据集汇总

时间:2011-12-28

来源:互联网

SQL code


如exec(@SelectResult) --这里面的列的个数是不固定的,有是会3列,有时会四列.个数是未知.

如exec(@SelectResult)这时的结果是这样
1  4.5  6
2  2.1  3
我想把上面的数据汇总一下.
结果为
1  4.5  6
2  2.1  3
3  6.6  9


作者: chirea   发布时间: 2011-12-28

不好做,最好在@SelectResult里直接统计

作者: ssp2009   发布时间: 2011-12-28

改 @SelectResult.

作者: qianjin036a   发布时间: 2011-12-28

引用 1 楼 ssp2009 的回复:
不好做,最好在@SelectResult里直接统计

比如:
@auto--自动的列,有时3个,有时四个
set @SelectResult='select a,+@auto+,c from tb'
如果是这样要怎么在@SelectResult里直接统计

作者: chirea   发布时间: 2011-12-28

SQL code

create table t
(
    id int,
    num int
)
insert into t
select 1,5 union all
select 2,6

declare @tab table
(
    id int,
    num int
)

insert into @tab
select * from t union all
select SUM(ID),SUM(num) from t

select * from @tab

--drop table t


作者: ju523756055   发布时间: 2011-12-28

lz能否在@SelectResult里直接用union all连接一下呢。。。

作者: dielianhua_chenlong   发布时间: 2011-12-28

SQL code
set @SelectResult='select a,'+@auto+',c from tb union all select max(a)+1,sum('
                     +REPLACE(@auto,',','),sum(')+'),max(c) from tb'

作者: Monkey__D__Luffy   发布时间: 2011-12-28

引用 5 楼 dielianhua_chenlong 的回复:
lz能否在@SelectResult里直接用union all连接一下呢。。。

可以呀,可是如何汇总才是一个难点呀,因为@auto列数不固定

作者: chirea   发布时间: 2011-12-28