日期:2014-05-17 浏览次数:20711 次
建议你提供详细的资料: 例如表的结构,表之间的关系,测试数据,相关算法及需要的结果。 这样有助于我们理解你的意思,更主要的是能尽快让你获得答案或解决问题的方法。
------解决方案--------------------
/* 楼主太懒了,给的表没用的字段都给了~~ 思路就是以下面的方式 如果不想给出指定的时间,用游标一条一条去判断是不是在这个时间范围之类。如果日期乱的话就没办法。 */ insert into WarnUse values ('0001','225779','EM','2010-07-23 15:45:14.000',null,null,null) insert into WarnUse values ('0001','225779','EM','2010-07-23 16:00:16.000',null,null,null) insert into WarnUse values ('0001','225779','EM','2010-07-24 19:45:17.000',null,null,null) insert into WarnUse values ('0001','225779','EM','2010-07-25 20:45:14.000',null,null,null) insert into WarnUse values ('0001','225779','EM','2010-07-25 20:00:16.000',null,null,null) insert into WarnUse values ('0001','225779','EM','2010-07-24 20:45:17.000',null,null,null) declare @SDT datetime,@EDT datetime set @SDT ='2010-07-25 20:00:00.000' set @EDT ='2010-07-25 21:00:00.000' select COUNT(*) from WarnUse where DATime between @SDT and @EDT ----------- 2 (1 row(s) affected) 没有看明白楼主怎么把这个值传到另外一个表里面去
------解决方案--------------------
insert into WarnCount(DADay,DANo,DAHour,SumQty)
select convert(varchar(8),DATime,112),convert(varchar(8),DATime,112)+convert(varchar(2),getdate(),108),convert(varchar(2),getdate(),108),count(1)
from WarnUse convert(varchar(8),DATime,112),convert(varchar(8),DATime,112)+convert(varchar(2),getdate(),108),convert(varchar(2),getdate(),108)
------解决方案--------------------
create table WarnUse(metertype varchar(4),datime datetime) insert into WarnUse values ('EM','2010-07-23 15:45:14.000') insert into WarnUse values ('EM','2010-07-23 16:00:16.000') insert into WarnUse values ('EM','2010-07-24 19:45:17.000') insert into WarnUse values ('EM','2010-07-25 20:45:14.000') insert into WarnUse values ('EM','2010-07-25 20:00:16.000') insert into WarnUse values ('EM','2010-07-24 20:45:17.000') create table WarnCount(dano varchar(10),metertype varchar(4),daday varchar(8),dahour varchar(2),sumqty int) go insert into WarnCount(metertype,DADay,DANo,DAHour,SumQty) select metertype,convert(varchar(8),DATime,112),convert(varchar(8),DATime,112)+convert(varchar(2),DATime,108),convert(varchar(2),DATime,108),count(1) from WarnUse m where not exists(select 1 from WarnCount n where n.metertype = m.metertype and n.DADay = convert(varchar(8),m.DATime,112) and