2张表分别为:数据是
TableAID Name Money Money777 OperateTime
01 小明 1 10 2011-9-15 13:24:00
01 小明 3 15 2011-9-15 13:25:00
01 小明 10 11 2011-9-15 15:19:00
03 小张 7 8 2011-9-15 15:18:00
03 小张 8 5 2011-9-13 15:18:00
07 小王 9 5   2011-9-17 15:18:00TableC
PeronID PeronName Money2 Money3 LogTime
01 小明 5 1 2011-9-15 13:24:00
01 小明 3 3 2011-9-15 13:25:00
01 小明 2 7 2011-9-15 15:19:00
03 小张 9 8 2011-9-15 10:19:00
03 小张 7 2 2011-9-15 15:52:00
07 小王 null null  nullselect
 a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3,c.Money777
from
(
select ID,Name,sum(money) as sum_money1
from tableA
where OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
group by ID,Name
) a left join   
(
select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
from tableC   
where LogTime>'2011-09-14' and LogTime<'2011-09-21'
group by PeronID ,PeronName
)b   
on
 a.ID=b.PeronID and a.name=b.PeronName
join TableA c
on
 a.ID=c.ID and a.name=c.name
where
  OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID  and OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
)----------------结果----------------------------
/* id   name sum_money1  sum_money2  sum_money3  Money777
---- ---- ----------- ----------- ----------- -----------
01   小明   14          10          11          11
03   小张   7           16          10          8
假如另外我还想列出小王的信息,数据为空处用缺数据表达,那么这句sql语句该如何写?

解决方案 »

  1.   

    也就是tableA和tableC有多少人,就列出几列数据
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2011-09-22 15:52:23
    -- Verstion:
    --      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) 
    -- Apr 22 2011 11:57:00 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
    --
    ----------------------------------------------------------------
    --> 测试数据:[TableA]
    if object_id('[TableA]') is not null drop table [TableA]
    go 
    create table [TableA]([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime)
    insert [TableA]
    select '01','小明',1,10,'2011-9-15 13:24:00' union all
    select '01','小明',3,15,'2011-9-15 13:25:00' union all
    select '01','小明',10,11,'2011-9-15 15:19:00' union all
    select '03','小张',7,8,'2011-9-15 15:18:00' union all
    select '03','小张',8,5,'2011-9-13 15:18:00' union all
    select '07','小王',9,5,'2011-9-17 15:18:00'
    --> 测试数据:[TableC]
    if object_id('[TableC]') is not null drop table [TableC]
    go 
    create table [TableC]([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime)
    insert [TableC]
    select '01','小明',5,1,'2011-9-15 13:24:00' union all
    select '01','小明',3,3,'2011-9-15 13:25:00' union all
    select '01','小明',2,7,'2011-9-15 15:19:00' union all
    select '03','小张',9,8,'2011-9-15 10:19:00' union all
    select '03','小张',7,2,'2011-9-15 15:52:00' union all
    select '07','小王',null,null,null
    --------------开始查询--------------------------
    select
     a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3,c.Money777
    from
    (
    select ID,Name,sum(money) as sum_money1
    from tableA
    where OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
    group by ID,Name
    ) a left join   
    (
    select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
    from tableC   
    where LogTime>'2011-09-14' and LogTime<'2011-09-21'
    group by PeronID ,PeronName
    )b   
    on
     a.ID=b.PeronID and a.name=b.PeronName
    left join TableA c
    on
     a.ID=c.ID and a.name=c.name
    where
      OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID and OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
    )
    ----------------结果----------------------------
    /* id   name sum_money1  sum_money2  sum_money3  Money777
    ---- ---- ----------- ----------- ----------- -----------
    01   小明   14          10          11          11
    03   小张   7           16          10          8
    07   小王   9           NULL        NULL        5(3 行受影响)*/
      

  3.   


    select
     a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3,c.Money777
    from
    (
    select ID,Name,sum(money) as sum_money1
    from tableA
    where OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
    group by ID,Name
    ) a left join   
    (
    select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
    from tableC   
    where LogTime>'2011-09-14' and LogTime<'2011-09-21'
    group by PeronID ,PeronName
    )b   
    on
     a.ID=b.PeronID and a.name=b.PeronName
    left join TableA c
    on
     a.ID=c.ID and a.name=c.name
    where
      OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID and OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
    )
      

  4.   

    select
     a.id,a.name,a.sum_money1,b.sum_money2,b.sum_money3,c.Money777
    from
    (
    select ID,Name,sum(money) as sum_money1
    from tableA
    where OperateTime>'2011-09-14' and OperateTime<'2011-09-21' 
    group by ID,Name
    ) a left join   
    (
    select PeronID ,PeronName ,sum(money2) as sum_money2,sum(money3) as sum_money3
    from tableC   
    where LogTime>'2011-09-14' and LogTime<'2011-09-21' or LogTime is null
    group by PeronID ,PeronName
    )b   
    on
     a.ID=b.PeronID and a.name=b.PeronName
    join TableA c
    on
     a.ID=c.ID and a.name=c.name
    where
      OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID and OperateTime>'2011-09-14' and OperateTime<'2011-09-21' 
    )
    这样试试