新!!!紧急求助!这样的存储过程如何编制!!
table1
--------------------------------------
_date e_name
09-03 A
09-03 B
09-03 C
09-04 A
09-04 B
09-04 D
09-04 E
09-05 B
09-05 E
09-06 A
09-06 D
09-06 E
09-07 B
09-07 E
-------------------------------------
table2
_date A B C D E
09-04 1 1 0 1 1
09-05 0 1 0 0 1
09-06 1 0 0 1 1
0代表‘没有’,1代表‘有’
--------------------------------
上一次提问,在e_name取值不确定时,在table1的基础上统计成table2样式,所用动态的sql语句
(paoluo(一天到晚游泳的鱼) )给出的。如下:
Declare @S Varchar(8000)
Select @S = 'Select _date '
Select @S = @S + ', Max(Case e_name When ' ' ' + e_name + ' ' ' Then 1 Else 0 End) As ' + e_name
From table1 Group By e_name
Select @S = @S + ' From table1 Group By _date '
EXEC(@S)
可是实现
------------------
现在的问题:
我想通过两个变量@d_b,@d_e,来向以上sql语句传递统计的起止时间,如@d_b=09-04,@d_e=09-06,又应该怎么编程???
------解决方案----------------------不好意思,刚才错了
--创建测试环境
create table table1(_date varchar(10),e_name varchar(10))
create table table2(_date varchar(10), A int, B int,C int,D int,E int)
--插入测试数据
insert table1(_date,e_name)
select '09-03 ', 'A ' union all
select '09-03 ', 'B ' union all
select '09-03 ', 'C ' union all
select '09-04 ', 'A ' union all
select '09-04 ', 'B ' union all
select '09-04 ', 'D ' union all
select '09-04 ', 'E ' union all
select '09-05 ', 'B ' union all
select '09-05 ', 'E ' union all
select '09-06 ', 'A ' union all
select '09-06 ', 'D ' union all
select '09-06 ', 'E ' union all
select '09-07 ', 'B ' union all
select '09-07 ', 'E '
insert table2(_date,A,B,C,D,E)
select '09-04 ', '1 ', '1 ', '0 ', '1 ', '1 ' union all
select '09-05 ', '0 ', '1 ', '0 ', '0 ', '1 ' union all
select '09-06 ', '1 ', '0 ', '0 ', '1 ', '1 '
--求解过程
Declare @S Varchar(8000),@d_b varchar(20),@d_e varchar(20)
Select @S = 'Select _date ',@d_b = '09-04 ',@d_e = '09-06 '
Select @S = @S + ', Max(Case e_name When ' ' ' + e_name + ' ' ' Then 1 Else 0 End) As ' + e_name
From table1 Group By e_name
Select @S = @S + ' From table1 where _date between ' ' '+@d_b + ' ' ' and ' ' ' + @d_e+ ' ' ' Group By _date '
exec (@S)
--删除测试环境
drop table table1,table2
/*--测试结果
_date A B C D E
---------- ----------- ----------- ----------- ----------- -----------
09-04 1 1 0 1 1
09-0