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:00
11 小李 null null nullTableC
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 null
11 小李 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假如另外我还想列出小李的信息,数据为空处用缺数据表达,那么这句sql语句该如何写?
也就是tableA和tableC有多少人,就列出几列数据
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:00
11 小李 null null nullTableC
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 null
11 小李 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假如另外我还想列出小李的信息,数据为空处用缺数据表达,那么这句sql语句该如何写?
也就是tableA和tableC有多少人,就列出几列数据
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 FULL 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
AND
OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID and OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
)
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 FULL 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 n
a.ID=b.PeronID and a.name=b.PeronName
left join TableA c
on
a.ID=c.ID and a.name=c.name
AND
OperateTime =(select MAX( OperateTime ) from TableA where ID=a.ID and OperateTime>'2011-09-14' and OperateTime<'2011-09-21'
)
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 10:05:45
-- 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' union all
select '11','小李',null,null,null
--> 测试数据:[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 union all
select '11','小李',null,null,null
--------------开始查询--------------------------
select
a.*,c.money2,c.money3,b.money777
from
(select id,name,SUM(money)money1 from tablea group by id,name)a
join
(select id,money777 from tablea a where not exists(select 1 from tablea where Id=a.ID and OperateTime>a.OperateTime))b
on
a.id=b.id
join
(select Peronid,SUM(money2)money2,SUM(money3)money3 from tableC group by Peronid)c
on
a.id=c.Peronid
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 14 10 11 11
03 小张 15 16 10 8
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。(4 行受影响)
*/