personID workType workQuantity workDate
这样的表每天存储了不同人的不同工作的工作量的统计,每天可能填写多次产生如下类似信息personID workType workQuantity workDate
041      1        2            2008-11-11
041      2        5            2008-11-11
042      3        3            2008-11-11
041      1        3            2008-11-12
041      1        4            2008-11-13
042      2        2            2008-11-13不同人可能干同一类型的工作。对于每个类型的工作都会有进度这样的统计比如 2008-11-11 041 对于1类型的工作干了 2个任务量,2008-11-12的时候干了1类型的3任务量,累计进度1任务量,2008-11-13的时候1类型干了4任务量,累计干了5任务量。昨天我问了可以按照天数统计当天总共的任务量,group by id,type,date。
但是好像没有办法计算出累积的任务量(计算同一个人同一个任务,每个日期之前的所有任务来那个的合,需要每天统计),不知道我的描述清晰不。
主要希望创建出这样的viewpersonID workType workQuantity  totalQuantity workDate
041      1        2             2               2008-11-11
041      1        3             5               2008-11-12
041      1        5             10               2008-11-13
042      3        3             3               2008-11-11 这个我原来想就做成一个表来处理,不过感觉系统中标太多了而且上述信息如果可以通过view创建就可以少创建重复的字段了。
         

解决方案 »

  1.   


    create view v1
    as
    select personid,worktype,workquantity,totalQuantity=(select sum(workquantity) from tb where personid=a.personid and worktype=a.worktype and workdate<=a.workdate),workdate
    from tb a
    group by personid,worktype,workquantity,workdate不过你给的结果数据好像有点问题
      

  2.   

    等一下如果personID workType workQuantity workDate 
    041      1        2            2008-11-11
    041      1        3            2008-11-11 --add new data here
    041      2        5            2008-11-11
    041      1        1            2008-11-11 --add new data here
    042      3        3            2008-11-11 
    041      1        3            2008-11-12 
    041      1        4            2008-11-13 
    042      2        2            2008-11-13 
    不知道你的语句对我心增加的数据支持度。如何?
      

  3.   

        id      type1   worktity           type2               workdate
    1 041 0423 2 2 1 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    2 041 0423 2 2 2 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    3 041 0423 2 2 1 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    4 041 0423 2 2 2 1 2008-12-11 0:00:00 2008-12-11 0:00:00 1
    5 041 0422 2 2 2 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    6 042 0424 5 3 1 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    7 042 0424 3 3 2 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    8 042 0424 5 3 1 1 2008-11-11 0:00:00 2008-11-1 0:00:00 1
    9 041 0422 2 2 2 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    10 041 0422 2 2 3 1 2008-12-11 0:00:00 2008-12-11 0:00:00 1
    11 041 0422 2 2 1 1 2008-11-11 0:00:00 2088-11-11 0:00:00 1
    12 041 0422 6 2 1 1 2008-11-11 0:00:00 2008-11-11 0:00:00 1
    NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
    你的语句执行完了是
            type1  type2     quantiy total  
    041 0422 1 2 8 2008-11-11 0:00:00//
    041 0422 1 6 8 2008-11-11 0:00:00//这两个就出问题了,应该是一条记录
    041 0422 2 2 4 2008-11-11 0:00:00
    041 0422 3 2 2 2008-12-11 0:00:00
    041 0423 1 2 4 2008-11-11 0:00:00
    041 0423 2 2 2 2008-11-11 0:00:00
    041 0423 2 2 4 2008-12-11 0:00:00
    042 0424 1 5 10 2008-11-11 0:00:00
    042 0424 2 3 3 2008-11-11 0:00:00
      

  4.   


    declare @t table(personid int,worktype int,workquantity int,workdate datetime)
    insert into @t select 041,1,2,'2008-11-11'
        union all  select 041,2,5,'2008-11-11'
        union all  select 042,3,3,'2008-11-11'
        union all  select 041,1,3,'2008-11-12'
        union all  select 041,1,4,'2008-11-13'
        union all  select 042,2,2,'2008-11-13'
    select a.personid,a.worktype,a.workquantity ,totalQuantity=(select sum(workquantity)from @t
    where personid=a.personid and worktype=a.worktype and workdate<=a.workdate),
    workdate from @t a
    group by a.personid,a.worktype,a.workdate,a.workquantity
    ---------------
      

  5.   

    041 1 2 2008-11-11 00:00:00.000
    041 1 3 2008-11-11 00:00:00.000
    041 1 1 2008-11-11 00:00:00.000看看你这三个数据,时间相同,personid相同,type相同,那就需要在增加一个列来把这三行数据分开,然后在select sum(workquantity) from tb where personid=a.personid and worktype=a.worktype and workdate<=a.workdate加上相应的判断条件就行了
      

  6.   

    select id=identity(int,1,1),tb.* into # from tb order by personid,worktype,workQuantity,workdate
    select personid,worktype,workquantity,totalQuantity=(select sum(workquantity) from # where personid=a.personid and worktype=a.worktype and workdate<=a.workdate and id<=a.id),workdate
    from # a
    group by personid,worktype,workquantity,workdate,id
      

  7.   

    看来没人能写出这个sql语句了