select * into #aa from tbl_AlarmHistory where year='2006' and type<3
要从tbl_AlarmHistory表中按year,month,type,num把中数据按临时表查出来,可以这样查SELECT count(*) FROM tbl_AlarmHistory group by year(iResTime),month(iResTime),iAlarmType 但是要每一条都要查,查完就存储临时表中,所以临时表中应该有48条数据
--trycreate table tbl_AlarmHistory([date] datetime, type int, num int) insert tbl_AlarmHistory select '2006-2-2', 2, 30 insert tbl_AlarmHistory select '2006-3-4', 2, 36 insert tbl_AlarmHistory select '2006-5-6', 1, 33 select [year]=2006, [month]=month([date]), type=3, num=sum(num) into #T from tbl_AlarmHistory where year([date])=2006 and type<3 group by month([date])select * from #T--result year month type num ----------- ----------- ----------- ----------- 2006 2 3 30 2006 3 3 36 2006 5 3 33(3 row(s) affected)drop table #T, tbl_AlarmHistory
select year(date) , month(date) , type , sum(dat) from tbl_AlarmHistory where year(date) = '2006' and type < 3 group by year(date) , month(date) , type ?
但是要每一条都要查,查完就存储临时表中,所以临时表中应该有48条数据
insert tbl_AlarmHistory select '2006-2-2', 2, 30
insert tbl_AlarmHistory select '2006-3-4', 2, 36
insert tbl_AlarmHistory select '2006-5-6', 1, 33
select [year]=2006, [month]=month([date]), type=3, num=sum(num) into #T
from tbl_AlarmHistory
where year([date])=2006 and type<3
group by month([date])select * from #T--result
year month type num
----------- ----------- ----------- -----------
2006 2 3 30
2006 3 3 36
2006 5 3 33(3 row(s) affected)drop table #T, tbl_AlarmHistory
select year(date) ,
month(date) ,
type ,
sum(dat)
from tbl_AlarmHistory
where year(date) = '2006'
and type < 3
group by year(date) ,
month(date) ,
type
?