原来发过一贴
http://topic.csdn.net/u/20100112/16/e13f3771-89a5-4148-b3ad-e97baa87d852.html
首先感谢原来发帖回复的各位
现在由于速度要求,整理一下,再发一贴求助..表A
aid, acontent
1, abc
2, bcd
3, efg
4, fgh
表B
bid,aid,btype, bcontent,modifydate
1, 1, 1, ABC, 2010-1-1
2, 1, 2, BCD, 2010-1-1
3, 1, 3, CDE, 2010-1-1
4, 4, 4, EFG, 2010-1-1
5, 1, 1, FGH, 2010-1-2A表和B表的关系是B表是A表的一个细节表. 表B的aid为关联的A表aid值,btype字段为:1,2,3,4四种类型. 一条表A记录最多和四条表B记录关联(分别为类型1,2,3,4的各一条),也可能一条不关联.就是表B没有相信的类型记录对应A表记录期待俩表的联合查询得到的结果为aid|acontent|bid_1 |bconten_1|bid_2 |bconten_2|bid_3|bconten_3|bid_4|bconten_4|
1 | abc | 5 | FGH | 2 | BCD | 3 | CDE | 空 | 空 |
2 | bcd | 空 | 空 | 空 | 空 | 空 | 空 | 空 | 空 |
3 | efg | 空 | 空 | 空 | 空 | 空 | 空 | 空 | 空 |
4 | fgh | 空 | 空 | 空 | 空 | 空 | 空 | 4 | EFG |每次选择出B表中时间最新的类型1,2,3,4的各一条和a表关联,就是如果aid 为1的话,在类型为1的情况下,b表可能有多条aid=1,btype=1的记录,选择出modifydate最新的一条和a表关联,如果b表没有则空一开始用了left join,在数据少时候,还可以接受.当a表有2000条.b表有8000条记录时,速度很慢,无法接受了,
看看大家有没有什么好的方法来处理这个问题
先谢谢了
http://topic.csdn.net/u/20100112/16/e13f3771-89a5-4148-b3ad-e97baa87d852.html
首先感谢原来发帖回复的各位
现在由于速度要求,整理一下,再发一贴求助..表A
aid, acontent
1, abc
2, bcd
3, efg
4, fgh
表B
bid,aid,btype, bcontent,modifydate
1, 1, 1, ABC, 2010-1-1
2, 1, 2, BCD, 2010-1-1
3, 1, 3, CDE, 2010-1-1
4, 4, 4, EFG, 2010-1-1
5, 1, 1, FGH, 2010-1-2A表和B表的关系是B表是A表的一个细节表. 表B的aid为关联的A表aid值,btype字段为:1,2,3,4四种类型. 一条表A记录最多和四条表B记录关联(分别为类型1,2,3,4的各一条),也可能一条不关联.就是表B没有相信的类型记录对应A表记录期待俩表的联合查询得到的结果为aid|acontent|bid_1 |bconten_1|bid_2 |bconten_2|bid_3|bconten_3|bid_4|bconten_4|
1 | abc | 5 | FGH | 2 | BCD | 3 | CDE | 空 | 空 |
2 | bcd | 空 | 空 | 空 | 空 | 空 | 空 | 空 | 空 |
3 | efg | 空 | 空 | 空 | 空 | 空 | 空 | 空 | 空 |
4 | fgh | 空 | 空 | 空 | 空 | 空 | 空 | 4 | EFG |每次选择出B表中时间最新的类型1,2,3,4的各一条和a表关联,就是如果aid 为1的话,在类型为1的情况下,b表可能有多条aid=1,btype=1的记录,选择出modifydate最新的一条和a表关联,如果b表没有则空一开始用了left join,在数据少时候,还可以接受.当a表有2000条.b表有8000条记录时,速度很慢,无法接受了,
看看大家有没有什么好的方法来处理这个问题
先谢谢了
a.aid , a.acontent,
max(case m.btype when 1 then cast(m.bid as varchar) else '' end) bid_1,
max(case m.btype when 1 then m.bcontent else '' end) bconten_1,
max(case m.btype when 2 then cast(m.bid as varchar) else '' end) bid_2,
max(case m.btype when 2 then m.bcontent else '' end) bconten_2,
max(case m.btype when 3 then cast(m.bid as varchar) else '' end) bid_3,
max(case m.btype when 3 then m.bcontent else '' end) bconten_3,
max(case m.btype when 4 then cast(m.bid as varchar) else '' end) bid_4,
max(case m.btype when 4 then m.bcontent else '' end) bconten_4
from
a ,
(select t.* from b t where not exists (select 1 from b where aid = t.aid and btype = t.btype and dt > t.dt)) m
where
a.aid = m.aid
group by
a.aid , a.acontent
--创建测试表
create table Table_A
(
aid int not null identity(1,1),
acontent varchar(4) not null
)create table Table_B
(
bid int not null identity(1,1),
aid int not null,
btype int not null,
bcontent varchar(4) not null,
modifydate datetime not null
)--填充数据
insert into
Table_A
(
acontent
)
select 'abc'
union all
select 'bcd'
union all
select 'efg'
union all
select 'fgh';insert into
Table_B
(
aid,
btype,
bcontent,
modifydate
)
select 1, 1, 'ABC', '2010-01-01'
union all
select 1, 2, 'BCD', '2010-01-01'
union all
select 1, 3, 'CDE', '2010-01-01'
union all
select 4, 4, 'EFG', '2010-01-01'
union all
select 1, 1, 'FGH', '2010-01-02';--查询,代码在SQL Server2005环境测试
With LatestB as
(
select
b.aid,
b.bid,
b.btype,
b.bcontent
from
Table_B b
where
b.modifydate = (select max(b1.modifydate) from Table_B b1 where b.aid = b1.aid and b.btype = b1.btype)
and
b.aid = (select max(b2.aid) from Table_B b2 where b.aid = b2.aid and b.btype = b2.btype and b.modifydate = b2.modifydate)
)
select
a.aid,a.acontent,
b1.bid,b1.bcontent,
b2.bid,b2.bcontent,
b3.bid,b3.bcontent,
b4.bid,b4.bcontent
from
Table_A a
left join
LatestB b1
on
a.aid = b1.aid
and
b1.btype = 1
left join
LatestB b2
on
a.aid = b2.aid
and
b2.btype = 2
left join
LatestB b3
on
a.aid = b3.aid
and
b3.btype = 3
left join
LatestB b4
on
a.aid = b4.aid
and
b4.btype = 4;
至于LEFT JOIN执行慢,没那么大数据量,不好测试。