数据是
TableA
ID 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
TableC
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
TableF
Id Name
01 小明
03 小张
07 小王
11 小李
15 小周
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语句该如何写?
也就是tableF有多少人,就列出几列数据
7 个解决方案
#1
用left 或者right join应该可以实现啊。
#2
如何实现?
#3
我疯了 楼主可不可以一次说完啊?
#4
呵呵,不好意思
#5
那麻烦小F了
#6
-- 这样?
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 13:47:47
-- 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
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4))
insert [TableF]
select '01','小明' union all
select '03','小张' union all
select '07','小王' union all
select '11','小李' union all
select '15','小周'
--------------开始查询--------------------------
select
isnull(a.ID,d.id) as id,isnull(a.Name,d.name) as name,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
full join
TableF d
on
a.Name=d.Name
----------------结果----------------------------
/* id name money2 money3 money777
---- ---- ----------- ----------- -----------
01 小明 10 11 11
03 小张 16 10 8
07 小王 NULL NULL 5
11 小李 NULL NULL NULL
15 小周 NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
*/
#7
--掉了 MONEY1
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 13:47:47
-- 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
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4))
insert [TableF]
select '01','小明' union all
select '03','小张' union all
select '07','小王' union all
select '11','小李' union all
select '15','小周'
--------------开始查询--------------------------
select
isnull(a.ID,d.id) as id,isnull(a.Name,d.name) as name,a.money1,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
full join
TableF d
on
a.Name=d.Name
----------------结果----------------------------
/* 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
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
*/
#1
用left 或者right join应该可以实现啊。
#2
如何实现?
#3
我疯了 楼主可不可以一次说完啊?
#4
呵呵,不好意思
#5
那麻烦小F了
#6
-- 这样?
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 13:47:47
-- 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
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4))
insert [TableF]
select '01','小明' union all
select '03','小张' union all
select '07','小王' union all
select '11','小李' union all
select '15','小周'
--------------开始查询--------------------------
select
isnull(a.ID,d.id) as id,isnull(a.Name,d.name) as name,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
full join
TableF d
on
a.Name=d.Name
----------------结果----------------------------
/* id name money2 money3 money777
---- ---- ----------- ----------- -----------
01 小明 10 11 11
03 小张 16 10 8
07 小王 NULL NULL 5
11 小李 NULL NULL NULL
15 小周 NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
*/
#7
--掉了 MONEY1
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-23 13:47:47
-- 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
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4))
insert [TableF]
select '01','小明' union all
select '03','小张' union all
select '07','小王' union all
select '11','小李' union all
select '15','小周'
--------------开始查询--------------------------
select
isnull(a.ID,d.id) as id,isnull(a.Name,d.name) as name,a.money1,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
full join
TableF d
on
a.Name=d.Name
----------------结果----------------------------
/* 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
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
*/