读书人

因前端实现太困难所以不得已改变数据

发布时间: 2012-01-18 00:23:26 作者: rapoo

因前端实现太困难,所以不得已改变数据库表结构,再次请教如何便这样的存储过程!!

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
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 '
GO
Declare @d_b smalldatetime, @d_e smalldatetime
Select @d_b = '2003-09-04 ', @d_e = '2003-09-06 '
Declare @S Varchar(8000)
Select @S = 'Insert table2 Select _date '
Select @S = @S + ', Max(Case e_name When ' ' ' + Name + ' ' ' Then N ' '是 ' ' Else ' '否 ' ' End) As ' + Name
From sysColumns Where ID = OBJECT_ID( 'table2 ') And Name != '_date ' Order By ColID
Select @S = @S + ' From table1 Where _date Between ' ' ' + Convert(Varchar(10), @d_b, 120) + ' ' ' And ' ' ' + Convert(Varchar(10), @d_e, 120) + ' ' ' Group By _date '
EXEC(@S)

Select * From table2
GO
Drop Table table1, table2
--Result
/*
_dateABCDEFG
2003-09-04 00:00:00是是否是是否否
2003-09-05 00:00:00否是否否是否否
2003-09-06 00:00:00是否否是是否否
*/

读书人网 >SQL Server

热点推荐