我创建了(按year,month,type,num)一个临时表,要从表tbl_AlarmHistory查询出2006年1-12月,类型小于3的数据按每查出一个月的数据再放入临时表中,应该怎么写?
表如下,应该有48条数据在里面
year  month  type num 
2006 1 3 45
……

解决方案 »

  1.   

    select * into #aa from tbl_AlarmHistory where year='2006' and type<3
      

  2.   

    要从tbl_AlarmHistory表中按year,month,type,num把中数据按临时表查出来,可以这样查SELECT count(*) FROM tbl_AlarmHistory group by year(iResTime),month(iResTime),iAlarmType
    但是要每一条都要查,查完就存储临时表中,所以临时表中应该有48条数据
      

  3.   

    --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
      

  4.   


    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 
    ?
      

  5.   

    5楼的大哥,提示错误Line 17: Incorrect syntax near 'month'.