数据是
TableA
ID Name Money Money777 OperateTime typeA
01 小明 1 10 2011-9-15 13:24:00 1
01 小明 3 15 2011-9-16 13:25:00 1
01 小明 10 11 2011-9-17 15:19:00 1
03 小张 7 8 2011-9-15 15:18:00 5
03 小张 8 5 2011-9-13 15:18:00 5
07 小王 9 5 2011-9-16 15:18:00 1
TableC
PeronID PeronName Money2 Money3 LogTime typeC
01 小明 5 1 2011-9-15 13:24:00 1
01 小明 3 3 2011-9-15 13:25:00 1
01 小明 2 7 2011-9-15 15:19:00 1
03 小张 9 8 2011-9-15 10:19:00 5
03 小张 7 2 2011-9-15 15:52:00 5
07 小王 null null null 1
TableF
Id Name type
01 小明 1
03 小张 5
07 小王 1
11 小李 1
15 小周 1
我想要的结果是能查询到当TableF中字段type=1的时候的所有人的数据(每个人只能有1行数据)
用这个sql语句可以查询到当TableF的字段type=1的时候所有人的数据
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, money777
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF
WHERE type = 1) d ON a.id = d.Id
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
但是其中小明的数据有两行,像这样的问题,如何在原有的SQL语句上稍做修改以达到每个人只有一行数据的效果?
21 个解决方案
#1
select a.* , c.* , f.*
from tablea a , tablec c tablef f
where f.type = 1 and f.id = a.id and f.id = c.peronid
and a.OperateTime = (select max(OperateTime) from tablea where id = a.id)
and c.LogTime = (select max(LogTime) from tablec where peronid = c.peronid)
select a.* , c.* , f.*
from tablea a , tablec c tablef f
where f.type = 1 and f.id = a.id and f.id = c.peronid
and not exists(select 1 from tablea where id = a.id and OperateTime > a.OperateTime)
and not exists(select 1 from tablec where peronid = c.peronid and LogTime > c.LogTime)
#2
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, money777
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF t
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) d ON a.id = d.Id
#3
小F你的SQL语句运行之后出错
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) d ON a.id = d.Id
似乎问题处在WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) 后少了个右扩号
加上右扩号之后
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type)) d ON a.id = d.Id
SQL语句运行结果是:
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
还是有两条小明的数据
#4
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-25 11:32:06
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[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,[typeA] int)
insert [TableA]
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
--> 测试数据:[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,[typeC] int)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4),[type] int)
insert [TableF]
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
--------------开始查询--------------------------
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 00:00:00' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, max(money777) as money777
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 00:00:00' AND
OperateTime <= '2011-9-16 23:59:59')group by ID) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF
WHERE type = 1) d ON a.id = d.Id
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
*/
#5
select f.id ,f.name,a.Money1,a.Money777 , c.Money2 , c.Money3 from tablef f
left join
(
select ID sum(Money) Money1 , sum(Money777) Money777 from tablea WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59')
group by id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
order by f.id
#6
;with cte as(
SELECT rid=row_number() over (order by getdate()),
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a
INNER JOIN
( SELECT id, money777 FROM tablea a
WHERE NOT EXISTS(
SELECT 1 FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime
AND OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id
INNER JOIN
( SELECT Peronid, SUM(money2) money2, SUM(money3) money3 FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid
FULL OUTER JOIN
( SELECT * FROM TableF
WHERE type = 1) d ON a.id = d.Id
)
/*select * from cte x where not exists(
select 1 from cte where id=x.id and name=x.name and rid<x.rid
)*/
select id,name,money1,money2,money3,money777 from cte where rid in (
select max(rid) from cte group by id,name)
/*
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
15 小周 NULL NULL NULL NULL
11 小李 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
#7
小 F和geniuswjt的SQL语句都能达到效果,但最好是取在时间限制范围内的Max(OperateTime),而不是时间限制范围内的Max(money777)
#8
因为关于money777我想要的结果是在时间限制范围内的Max(OperateTime)这一行所对应的money777
#9
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
WHERE typeA = 1 AND OperateTime >= '2006-8-23 00:00:00' AND OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a
INNER 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
INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3 FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid
FULL OUTER JOIN
(SELECT * FROM TableF
WHERE type = 1) d ON a.id = d.Id
/*
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
#10
SELECT ISNULL(a.id, d.Id) AS id, ISNULL(a.name, d.Name) AS name, a.money1, c.money2, OperateTime,
c.money3, b.money777
FROM (SELECT id, name, SUM(money) money1
FROM tablea
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, money777,OperateTime
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF
WHERE type = 1) d ON a.id = d.Id
/*你那句把时间放出来,你看一下,小明的在时间限制范围内的Max(OperateTime)这一行所对应的money777就是上面的结果其实
id name money1 money2 OperateTime money3 money777
---- ---- ----------- ----------- ----------------------- ----------- -----------
01 小明 4 10 2011-09-16 13:25:00.000 11 15
01 小明 4 10 2011-09-17 15:19:00.000 11 11
07 小王 9 NULL 2011-09-16 15:18:00.000 NULL 5
15 小周 NULL NULL NULL NULL NULL
11 小李 NULL NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
#11
4,10,11,11
#12
不是啊,在时间范围'2006-8-23 0:0:0' 和 '2011-9-16 23:59:59'之内的Max(OperateTime)是
01 小明 3 15 2011-9-16 13:25:00 1
TableA
ID Name Money Money777 OperateTime typeA
01 小明 1 10 2011-9-15 13:24:00 1
01 小明 3 15 2011-9-16 13:25:00 1
01 小明 10 11 2011-9-17 15:19:00 1
03 小张 7 8 2011-9-15 15:18:00 5
03 小张 8 5 2011-9-13 15:18:00 5
07 小王 9 5 2011-9-16 15:18:00 1
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
#13
你的这句SQL语句中小明的数据有2条
#14
汗,这是给你看你的语句带上时间后是什么样的而已,不是用来查结果的。
#15
确实,带上时间范围后会出现这个或那个的问题,那么这个SQL语句应该如何修改呢?
#16
我想要的最终结果是:
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
或者,geniuswjt,你也可以用你的SQL语句写出这个结果
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
或者,geniuswjt,你也可以用你的SQL语句写出这个结果
#17
怎么办啊?
#18
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select ID ,sum(Money) Money1 , sum(Money777) Money777 from tablea WHERE
OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59'
group by id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 25
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#19
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select m.* , n.Money777 from
(select a.ID ,sum(a.Money) Money1 from tablea a WHERE a.OperateTime >= '2006-8-23 0:0:0' AND a.OperateTime <= '2011-9-16 23:59:59' group by id) m,
(select a.id , a.Money777 from tablea a where OperateTime = (select max(OperateTime) from tablea where OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59' and id = a.id)) n
where m.id = n.id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#20
--19楼行,如下稍微简单点.
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select a.ID ,sum(a.Money) Money1 ,b.Money777 from tablea a ,tablea b WHERE
a.OperateTime >= '2006-8-23 0:0:0' AND a.OperateTime <= '2011-9-16 23:59:59' and a.id = b.id and
b.OperateTime = (select max(OperateTime) from tablea where id = b.id and OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59' )
group by a.id , b.Money777
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#21
谢谢!!!
#1
select a.* , c.* , f.*
from tablea a , tablec c tablef f
where f.type = 1 and f.id = a.id and f.id = c.peronid
and a.OperateTime = (select max(OperateTime) from tablea where id = a.id)
and c.LogTime = (select max(LogTime) from tablec where peronid = c.peronid)
select a.* , c.* , f.*
from tablea a , tablec c tablef f
where f.type = 1 and f.id = a.id and f.id = c.peronid
and not exists(select 1 from tablea where id = a.id and OperateTime > a.OperateTime)
and not exists(select 1 from tablec where peronid = c.peronid and LogTime > c.LogTime)
#2
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, money777
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF t
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) d ON a.id = d.Id
#3
小F你的SQL语句运行之后出错
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) d ON a.id = d.Id
似乎问题处在WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type) 后少了个右扩号
加上右扩号之后
WHERE type = 1 and not exists(select 1 from tablef where name=t.name and type<t.type)) d ON a.id = d.Id
SQL语句运行结果是:
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
还是有两条小明的数据
#4
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-09-25 11:32:06
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[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,[typeA] int)
insert [TableA]
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
--> 测试数据:[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,[typeC] int)
insert [TableC]
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
--> 测试数据:[TableF]
if object_id('[TableF]') is not null drop table [TableF]
go
create table [TableF]([Id] varchar(2),[Name] varchar(4),[type] int)
insert [TableF]
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
--------------开始查询--------------------------
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 00:00:00' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, max(money777) as money777
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 00:00:00' AND
OperateTime <= '2011-9-16 23:59:59')group by ID) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF
WHERE type = 1) d ON a.id = d.Id
----------------结果----------------------------
/* id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
*/
#5
select f.id ,f.name,a.Money1,a.Money777 , c.Money2 , c.Money3 from tablef f
left join
(
select ID sum(Money) Money1 , sum(Money777) Money777 from tablea WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59')
group by id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
order by f.id
#6
;with cte as(
SELECT rid=row_number() over (order by getdate()),
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
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a
INNER JOIN
( SELECT id, money777 FROM tablea a
WHERE NOT EXISTS(
SELECT 1 FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime
AND OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id
INNER JOIN
( SELECT Peronid, SUM(money2) money2, SUM(money3) money3 FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid
FULL OUTER JOIN
( SELECT * FROM TableF
WHERE type = 1) d ON a.id = d.Id
)
/*select * from cte x where not exists(
select 1 from cte where id=x.id and name=x.name and rid<x.rid
)*/
select id,name,money1,money2,money3,money777 from cte where rid in (
select max(rid) from cte group by id,name)
/*
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
15 小周 NULL NULL NULL NULL
11 小李 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
#7
小 F和geniuswjt的SQL语句都能达到效果,但最好是取在时间限制范围内的Max(OperateTime),而不是时间限制范围内的Max(money777)
#8
因为关于money777我想要的结果是在时间限制范围内的Max(OperateTime)这一行所对应的money777
#9
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
WHERE typeA = 1 AND OperateTime >= '2006-8-23 00:00:00' AND OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a
INNER 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
INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3 FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid
FULL OUTER JOIN
(SELECT * FROM TableF
WHERE type = 1) d ON a.id = d.Id
/*
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 11
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(4 行受影响)
#10
SELECT ISNULL(a.id, d.Id) AS id, ISNULL(a.name, d.Name) AS name, a.money1, c.money2, OperateTime,
c.money3, b.money777
FROM (SELECT id, name, SUM(money) money1
FROM tablea
WHERE 1 = 1 AND typeA = 1 AND OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59'
GROUP BY id, name) a INNER JOIN
(SELECT id, money777,OperateTime
FROM tablea a
WHERE NOT EXISTS
(SELECT 1
FROM tablea
WHERE Id = a.ID AND OperateTime > a.OperateTime AND
OperateTime >= '2006-8-23 0:0:0' AND
OperateTime <= '2011-9-16 23:59:59')) b ON a.id = b.id INNER JOIN
(SELECT Peronid, SUM(money2) money2, SUM(money3) money3
FROM tableC
GROUP BY Peronid) c ON a.id = c.Peronid FULL OUTER JOIN
(SELECT *
FROM TableF
WHERE type = 1) d ON a.id = d.Id
/*你那句把时间放出来,你看一下,小明的在时间限制范围内的Max(OperateTime)这一行所对应的money777就是上面的结果其实
id name money1 money2 OperateTime money3 money777
---- ---- ----------- ----------- ----------------------- ----------- -----------
01 小明 4 10 2011-09-16 13:25:00.000 11 15
01 小明 4 10 2011-09-17 15:19:00.000 11 11
07 小王 9 NULL 2011-09-16 15:18:00.000 NULL 5
15 小周 NULL NULL NULL NULL NULL
11 小李 NULL NULL NULL NULL NULL
警告: 聚合或其他 SET 操作消除了 Null 值。
(5 行受影响)
#11
4,10,11,11
#12
不是啊,在时间范围'2006-8-23 0:0:0' 和 '2011-9-16 23:59:59'之内的Max(OperateTime)是
01 小明 3 15 2011-9-16 13:25:00 1
TableA
ID Name Money Money777 OperateTime typeA
01 小明 1 10 2011-9-15 13:24:00 1
01 小明 3 15 2011-9-16 13:25:00 1
01 小明 10 11 2011-9-17 15:19:00 1
03 小张 7 8 2011-9-15 15:18:00 5
03 小张 8 5 2011-9-13 15:18:00 5
07 小王 9 5 2011-9-16 15:18:00 1
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
#13
你的这句SQL语句中小明的数据有2条
#14
汗,这是给你看你的语句带上时间后是什么样的而已,不是用来查结果的。
#15
确实,带上时间范围后会出现这个或那个的问题,那么这个SQL语句应该如何修改呢?
#16
我想要的最终结果是:
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
或者,geniuswjt,你也可以用你的SQL语句写出这个结果
也就是说我想要的最后的结果应该是:
id name money1 money2 money3 money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
或者,geniuswjt,你也可以用你的SQL语句写出这个结果
#17
怎么办啊?
#18
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select ID ,sum(Money) Money1 , sum(Money777) Money777 from tablea WHERE
OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59'
group by id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 25
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#19
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select m.* , n.Money777 from
(select a.ID ,sum(a.Money) Money1 from tablea a WHERE a.OperateTime >= '2006-8-23 0:0:0' AND a.OperateTime <= '2011-9-16 23:59:59' group by id) m,
(select a.id , a.Money777 from tablea a where OperateTime = (select max(OperateTime) from tablea where OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59' and id = a.id)) n
where m.id = n.id
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#20
--19楼行,如下稍微简单点.
create table TableA([ID] varchar(2),[Name] varchar(4),[Money] int,[Money777] int,[OperateTime] datetime,[typeA] int)
insert TableA
select '01','小明',1,10,'2011-9-15 13:24:00',1 union all
select '01','小明',3,15,'2011-9-16 13:25:00',1 union all
select '01','小明',10,11,'2011-9-17 15:19:00',1 union all
select '03','小张',7,8,'2011-9-15 15:18:00',5 union all
select '03','小张',8,5,'2011-9-13 15:18:00',5 union all
select '07','小王',9,5,'2011-9-16 15:18:00',1
create table TableC([PeronID] varchar(2),[PeronName] varchar(4),[Money2] int,[Money3] int,[LogTime] datetime,[typeC] int)
insert Tablec
select '01','小明',5,1,'2011-9-15 13:24:00',1 union all
select '01','小明',3,3,'2011-9-15 13:25:00',1 union all
select '01','小明',2,7,'2011-9-15 15:19:00',1 union all
select '03','小张',9,8,'2011-9-15 10:19:00',5 union all
select '03','小张',7,2,'2011-9-15 15:52:00',5 union all
select '07','小王',null,null,null,null
create table Tablef([Id] varchar(2),[Name] varchar(4),[type] int)
insert Tablef
select '01','小明',1 union all
select '03','小张',5 union all
select '07','小王',1 union all
select '11','小李',1 union all
select '15','小周',1
go
select f.id ,f.name,a.Money1, c.Money2 , c.Money3,a.Money777 from tablef f
left join
(
select a.ID ,sum(a.Money) Money1 ,b.Money777 from tablea a ,tablea b WHERE
a.OperateTime >= '2006-8-23 0:0:0' AND a.OperateTime <= '2011-9-16 23:59:59' and a.id = b.id and
b.OperateTime = (select max(OperateTime) from tablea where id = b.id and OperateTime >= '2006-8-23 0:0:0' AND OperateTime <= '2011-9-16 23:59:59' )
group by a.id , b.Money777
) a on f.id = a.id
left join
(
select PeronID , sum(Money2) Money2, sum(Money3) Money3 from tablec group by peronid
) c
on f.id = c.peronid
where f.type = 1
order by f.id
drop table TableA , Tablec , Tablef
/*
id name Money1 Money2 Money3 Money777
---- ---- ----------- ----------- ----------- -----------
01 小明 4 10 11 15
07 小王 9 NULL NULL 5
11 小李 NULL NULL NULL NULL
15 小周 NULL NULL NULL NULL
(所影响的行数为 4 行)
*/
#21
谢谢!!!