- 爱易网页
 
                        - 
                            MSSQL教程
 
                        - 因前端实现太困难,所以不得已改变数据库表结构,再次请问怎么便这样的存储过程! 
 
                         
                    
                    
                    日期:2014-05-19  浏览次数:20701 次 
                    
                        
                         因前端实现太困难,所以不得已改变数据库表结构,再次请教如何便这样的存储过程!!
 create   table   table1(_date   smalldatetime,e_name   varchar(10)) 
 create   table   table2(_date   smalldatetime,A   varchar(6),   B   varchar(6),C            varchar(6),D   varchar(6),E   varchar(6),F   varchar(6),G   varchar(6)) 
  
 --插入测试数据 
 insert   table1(_date,e_name) 
 select    '2003-09-03 ', 'A '   union   all 
 select    '2003-09-03 ', 'B '   union   all 
 select    '2003-09-03 ', 'C '   union   all 
 select    '2003-09-04 ', 'A '   union   all 
 select    '2003-09-04 ', 'B '   union   all 
 select    '2003-09-04 ', 'D '   union   all 
 select    '2003-09-04 ', 'E '   union   all 
 select    '2003-09-05 ', 'B '   union   all 
 select    '2003-09-05 ', 'E '   union   all 
 select    '2003-09-06 ', 'A '   union   all 
 select    '2003-09-06 ', 'D '   union   all 
 select    '2003-09-06 ', 'E '   union   all 
 select    '2003-09-07 ', 'B '   union   all 
 select    '2003-09-07 ', 'E ' 
  
 table1现有数据如下 
 -------------------------------------- 
 produ_date   e_name    
 2003-09-03   A 
 2003-09-03   B 
 2003-09-03   C 
 2003-09-04   A 
 2003-09-04   B 
 2003-09-04   D 
 2003-09-04   E 
 2003-09-05   B 
 2003-09-05   E 
 2003-09-06   A 
 2003-09-06   D 
 2003-09-06   E 
 2003-09-07   B 
 2003-09-07   E 
 ----------------------------- 
 统计成如table2的样式的将统计结果插入到已有表table2中,并且用@d_b   smalldatetime,@d_e   smalldatetime两个变量来向这个存储过程传递统计的起止时间,如@d_b= '2003-09-04 ',@d_e= '2003-09-06 ',则可以得到: 
 -------- 
 table2 
 produ_date   A   B   C   D   E   F   G 
 2003-09-04   1   1   0   1   1   0   0 
 2003-09-05   0   1   0   0   1   0   0 
 2003-09-06   1   0   0   1   1   0   0 
 0代表‘没有’,1代表‘有’ 
 ----------- 
 非常感谢,上次提问中给出答案的mengmou()mengmou()   和paoluo(一天到晚游泳的鱼)    
  
 这次小弟试验了以上午也没有结果,只好求大家帮忙了
------解决方案--------------------
select Produ_date,sum(case e_name when  'A ' then 1 else null end) as A , 
                   sum(case e_name when  'B ' then 1 else null end) as B , 
                   sum(case e_name when  'C ' then 1 else null end) as C , 
                   sum(case e_name when  'D ' then 1 else null end) as D , 
                   sum(case e_name when  'E ' then 1 else null end) as E  
 from Table1  
 group by Produ_date 
 没有测试
------解决方案--------------------
create table table1(_date smalldatetime,e_name varchar(10)) 
 create table table2(_date smalldatetime,A varchar(6), B varchar(6),C    varchar(6),D varchar(6),E varchar(6),F varchar(6),G varchar(6)) 
  
 --插入测试数据 
 insert table1(_date,e_name) 
 select  '2003-09-03 ', 'A ' union all 
 select  '2003-09-03 ', 'B ' union all 
 select  '2003-09-03 ', 'C ' union all 
 select  '2003-09-04 ', 'A ' union all 
 select  '2003-09-04 ', 'B ' union all