我想得到C D 北京-飞机 北京-火车 北京-汽车 汇总 广州-飞机 广州-火车 广州-汽车..
2002 1号 100 500 500 1100 ...
2002 2号
....解释:我的A,B列不是固定的,需要得到每年每个柜员在A列(城市)中出现的,每个B列(交通工具)中的统计,并且B列中A列的每个类都进行汇总,即C D | 北京 | 广州 | 上海 |
| 飞机 火车 汽车 汇总 | 飞机 火车 汽车 汇总| 飞机 火车 汽车 汇总|
2002 1号
2002 2号
这次多了汇总!!!
2002 1号 100 500 500 1100 ...
2002 2号
....解释:我的A,B列不是固定的,需要得到每年每个柜员在A列(城市)中出现的,每个B列(交通工具)中的统计,并且B列中A列的每个类都进行汇总,即C D | 北京 | 广州 | 上海 |
| 飞机 火车 汽车 汇总 | 飞机 火车 汽车 汇总| 飞机 火车 汽车 汇总|
2002 1号
2002 2号
这次多了汇总!!!
declare @s varchar(8000)
set @s=''
select @s=@s+'
,['+A+case sid when 0
then '-'+B+']=sum(case when a='''+A+''' and B='''+B+''' then E else 0 end)'
else '-汇总]=sum(case when a='''+A+''' then E else 0 end)' end
from(
select a,b,sid=0 from 表 group by a,b
union all
select a,'',1 from 表 group by a
)a order by a,sid
exec('select C,D'+@s+'
from 表
group by C,D
order by C,D')
create table 表(A varchar(10),B varchar(10),C varchar(10),D varchar(10),E int)
insert 表 select '北京','飞机','2002','1号',100
union all select '广州','火车','2003','2号',200
union all select '上海','汽车','2004','3号',300
union all select '北京','火车','2002','3号',500
union all select '广州','汽车','2003','1号',600
union all select '上海','飞机','2004','2号',700
union all select '北京','汽车','2002','3号',500
union all select '广州','飞机','2003','1号',600
union all select '上海','火车','2004','2号',700
go--查询
declare @s varchar(8000)
set @s=''
select @s=@s+'
,['+A+case sid when 0
then '-'+B+']=sum(case when a='''+A+''' and B='''+B+''' then E else 0 end)'
else '-汇总]=sum(case when a='''+A+''' then E else 0 end)' end
from(
select a,b,sid=0 from 表 group by a,b
union all
select a,'',1 from 表 group by a
)a order by a,sid
exec('select C,D'+@s+'
from 表
group by C,D
order by C,D')
go--删除测试
drop table 表/*--测试结果
C D 北京-飞机 北京-火车 北京-汽车 北京-汇总 广州-飞机 广州-火车 广州-汽车 广州-汇总 上海-飞机 上海-火车 上海-汽车 上海-汇总
---------- ---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
2002 1号 100 0 0 100 0 0 0 0 0 0 0 0
2002 3号 0 500 500 1000 0 0 0 0 0 0 0 0
2003 1号 0 0 0 0 600 0 600 1200 0 0 0 0
2003 2号 0 0 0 0 0 200 0 200 0 0 0 0
2004 2号 0 0 0 0 0 0 0 0 700 700 0 1400
2004 3号 0 0 0 0 0 0 0 0 0 0 300 300--*/
如何将数组带入存储过程?
例如:客户端A列选了北京,上海,并且B列只查看飞机,火车,请问
我如何将这些数据带入存储过程?
还是在客户端完成sql语句的编写,一次进行查询???
我现在是在数据中间加标识符,如"— \"等符号进行区分,到sql中再解开,但是sql中没有数组的说法,请问你们是如何做的。
例:我想将A中的数据发到sql端,A中的数据可变,sql端如何处理!!
create proc p_qry
@str varchar(1000)
as
declare @s varcahr(8000)
set @s=case @str when '' then '' else ' where '''+replace(@str,',',''',''')+''''exec('select * from xx '+@s)
go
--调用示例:
exec p_qry 'a1,a2,a3,a4,a5'
我再一次向你致敬!我是SQL新手,以后还请你多多指教!谢谢!
table(a1,a1num,a2,a2num,a3,a3num)
NAME NUM
我想得到 a1 a1num
a2 a2num
a3 a3num