--> 测试数据:[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
isnull(a.id,c.id) as id,isnull(a.name,c.name) as name,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
full 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')
and
OperateTime is not null
union all
select
ID,Name, sum_money1, sum_money2, sum_money3,Money777
from
(
select ID,Name,Money as sum_money1,'' as sum_money2, '' as sum_money3,Money777,OperateTime from TableA
union all
select PeronID,PeronName,'' as sum_money1,[Money2],[Money3],'',LogTime from tablec)t
where
OperateTime is null
----------------结果----------------------------
/* id name sum_money1 sum_money2 sum_money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 14 10 11 11
03 小张 7 16 10 8
07 小王 9 NULL NULL 5
11 小李 NULL 0 0 NULL
07 小王 0 NULL NULL 0
11 小李 0 NULL NULL 0
这条sql语句的效果是将tableA和tableC中所有人(哪怕这个人的其他数据是否为空,哪怕是全部为null)全部列出来
但是sql语句的执行结果是:小王和小李各有两条重复数据
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
isnull(a.id,c.id) as id,isnull(a.name,c.name) as name,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
full 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')
and
OperateTime is not null
union all
select
ID,Name, sum_money1, sum_money2, sum_money3,Money777
from
(
select ID,Name,Money as sum_money1,'' as sum_money2, '' as sum_money3,Money777,OperateTime from TableA
union all
select PeronID,PeronName,'' as sum_money1,[Money2],[Money3],'',LogTime from tablec)t
where
OperateTime is null
----------------结果----------------------------
/* id name sum_money1 sum_money2 sum_money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 14 10 11 11
03 小张 7 16 10 8
07 小王 9 NULL NULL 5
11 小李 NULL 0 0 NULL
07 小王 0 NULL NULL 0
11 小李 0 NULL NULL 0
这条sql语句的效果是将tableA和tableC中所有人(哪怕这个人的其他数据是否为空,哪怕是全部为null)全部列出来
但是sql语句的执行结果是:小王和小李各有两条重复数据
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'
)
ISNULL(a.ID,b.ID) AS ID,
ISNULL(a.NAME,b.NAME) AS NAME,
a.sum_money1,
b.sum_money2,
a.Money777
FROM
(select ID,Name,sum(money) as sum_money1,MAX(Money777) AS Money777 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) AS b ON a.ID=b.PeronID AND a.NAME=b.PeronName
-- 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 行受影响)
*/