数据库中几百W条记录
表A如下:
日期         字段1     字段2    字段3    字段4
2007-01-01   a           n      a         a
...........................................
每天有上千条记录日累计
insert into b select 日期,sum(字段1),sum(字段2),sum(字段3),sum(字段4)from a  where 日期='用户选择的日期'
表B结构同A
(每一天都有累计)
月累计
insert into c select 日期,sum(字段1),sum(字段2),sum(字段3),sum(字段4)
from b where 日期<='用户选择的日期' and 日期>='该月的1号(程序中处理)'
表C结构同A
(每一天都有截止到‘用户选择的日期'的累计)
年累计
该怎么写呀也要写入表D中,表D结构与A相同
要求:(每一天都有截止到‘用户选择的日期'的累计)
年累计时不能简单的直接累计月累计好的数据,
只能累计‘用户选择的日期所在月分’以前每月的最后一天的数据和’用户选择的日期’这一天的数据,才是年累计的数据,

解决方案 »

  1.   

    本月1号
    convert(varchar(7),getdate(),120)+'-1'
      

  2.   

    select * from time_dimension a where  
    not exists(select 1 from time_dimension where datename(yy,a.the_date)=datename(yy,the_date) and  datename(mm,a.the_date)=datename(mm,the_date) and a.the_date < the_date)
    and datediff(mm,a.the_date,getdate()) BETWEEN 1 and 11
    or  datediff(dd,a.the_date,getdate())=0
    order by time_id--getdate() 为用户选择的日期
    --time_dimension 为你说的表A
      

  3.   

    insert into c select year(日期),sum(字段1),sum(字段2),sum(字段3),sum(字段4)
    from b where year(日期)=year('用户选择的日期') and right(cast(日期 as varchar(10)), 2)<=1