两表:
tbl_a
序号 日期 数量
1 2006-6-22 6
2 2006-7-23 5
3 2006-8-24 4tbl_b
序号 日期 数量
1 2006-6-22 6
2 2006-6-21 5
3 2006-6-20 4
4 2006-7-22 6
5 2006-7-21 5
6 2006-7-20 4结果: tbl_a_序号 tbl_a_日期 tbl_a数量 日期前数量
1 2006-6-22 6 15
2 2006-7-23 5 30
3 2006-8-24 4 30关系:日期前数量=tbl_b表中日期<tbl_a表中日前的所有数量的"合计" 要求:一个SQL语句.

解决方案 »

  1.   

    Select 
    A.序号,
    A.日期,
    A.数量,
    SUM(B.数量) As 日期前数量
    From 
    tbl_a A
    Left Join tbl_b
    On A.日期>B.日期
    Group By A.序号,A.日期,A.数量
      

  2.   

    select
        a.序号,a.日期,a.数量,sum(b.数量)
    from
        tbl_a a,tbl_b b
    where
        a.2006-6-22>=b.2006-6-22
    group by
        a.序号,a.日期,a.数量
      

  3.   

    declare @t table(id int ,rq datetime,quan int)
    insert @t select 1,'2006-6-22',6
    union all select 2,'2006-7-23',5
    union all select 3,'2006-8-24',4
    declare @t1 table(id int ,rq datetime,quan int)
    insert @t1 select 1,'2006-6-22',6
    union all select 2,'2006-6-21',5
    union all select 3,'2006-6-20',4
    union all select 4,'2006-7-22',6
    union all select 5,'2006-7-21',5
    union all select 6,'2006-7-20',4select A.*,
    数量=(select sum(quan) from @t1 where rq<=A.rq)from @t A
      

  4.   

    上面有點問題,改下
    Select 
    A.序号,
    A.日期,
    A.数量,
    SUM(B.数量) As 日期前数量
    From 
    tbl_a A
    Left Join tbl_b B
    On A.日期>=B.日期
    Group By A.序号,A.日期,A.数量Create Table tbl_a
    (序号 Int,
     日期 Varchar(10),
     数量 Int)
    Insert tbl_a Select 1, '2006-6-22', 6
    Union All Select 2, '2006-7-23', 5
    Union All Select 3, '2006-8-24', 4

    Create Table tbl_b
    (序号 Int,
     日期 Varchar(10),
     数量 Int)
    Insert tbl_b Select 1, '2006-6-22', 6
    Union All Select 2, '2006-6-21', 5
    Union All Select 3, '2006-6-20', 4
    Union All Select 4, '2006-7-22', 6
    Union All Select 5, '2006-7-21', 5
    Union All Select 6, '2006-7-20', 4
    GO
    Select 
    A.序号,
    A.日期,
    A.数量,
    SUM(B.数量) As 日期前数量
    From 
    tbl_a A
    Left Join tbl_b B
    On A.日期>=B.日期
    Group By A.序号,A.日期,A.数量
    GO
    Drop Table tbl_a,tbl_b
    GO
    /*
    序号 日期 数量 日期前数量
    1 2006-6-22 6 15
    2 2006-7-23 5 30
    3 2006-8-24 4 30
    */
      

  5.   

    樓主的要求應該改下。关系:日期前数量=tbl_b表中日期<=tbl_a表中日前的所有数量的"合计"
    用關聯比用子查詢要好些
      

  6.   

    Create Table tbl_a
    (序号 Int,
     日期 Varchar(10),
     数量 Int)
    Insert tbl_a Select 1, '2006-6-22', 6
    Union All Select 2, '2006-7-23', 5
    Union All Select 3, '2006-8-24', 4

    Create Table tbl_b
    (序号 Int,
     日期 Varchar(10),
     数量 Int)
    Insert tbl_b Select 1, '2006-6-22', 6
    Union All Select 2, '2006-6-21', 5
    Union All Select 3, '2006-6-20', 4
    Union All Select 4, '2006-7-22', 6
    Union All Select 5, '2006-7-21', 5
    Union All Select 6, '2006-7-20', 4select 序号,日期,数量,(select sum(数量) from tbl_b where datediff(dd,a.日期,日期)<=0) as 数量
    from tbl_a A
      

  7.   

    基本解决.小弟先谢了.不过有一小问.假如 tbl_a 表中存在"日期前数量"字段update 语句如何写.还是一样的吗?
      

  8.   

    一样的,将"日期前数量"字段 UPDATE成上面几位写的那个SUM值就可以了。
      

  9.   

    update tbl_a
    set
        日期前数量=isnull((select sum(数量) from tbl_b where rq<=tbl_a.rq),0)
      

  10.   

    这几天,好像问类似问题的多了。
    行转列,求TREE节点,之类的慢慢转成这种问题了。http://community.csdn.net/Expert/topic/4836/4836185.xml?temp=.9146692
    http://community.csdn.net/Expert/topic/4837/4837698.xml?temp=.3161127