求SQL 两表数据提取语句(200)

C

cvfn

Unregistered / Unconfirmed
GUEST, unregistred user!
有表A和表B 现需按表A编号找表B中相同编号的字段,按结束日期排序,并提取结束日期小于99999999第一条录数据保存在表A中表A:编号 开始日期 结束日期 数值201 80202 20204 30 表B编号 开始日期 结束日期 201 20100123 20100202201 20100203 20100323 201 20100324 20101216 需要提取这条 201 20101217 99999999 202 20100612 20100716 需要提取这条 202 20100718 99999999203 20100518 99999999204 20100412 20100510 204 20100511 20100615204 20100616 20100718 需要提取这条 204 20100719 99999999最后运行结果是:表A编号 开始日期 结束日期 数值201 20100324 20101216 80202 20100612 20100716 20204 20100616 20100718 30
 
select 编号,前一次开始日期,前次数值,当前开始日期,当前数值 from(select 编号,前一次开始日期,前次数值 from table where 条件) C,(select 编号,当前开始日期,当前数值 from table where 条件) Dwhere C.编号=D.编号(+) AND 其他条件意思我阐明了,看不懂就不说了。。。
 
select a.*,c.dt1,c.dt2 from test2 aleft join ( select bh,Max(dt2) dt2 from test1 where dt2<>'99999999' group by bh ) as b on a.bh=b.bhleft join test1 c on b.bh=c.bh and b.dt2=c.dt2/*create table test1(bh varchar(10),dt1 varchar(10),dt2 varchar(10))insert into test1(bh,dt1,dt2) values('201','20100123','20100202')insert into test1(bh,dt1,dt2) values('201','20100123','20100202')insert into test1(bh,dt1,dt2) values('201','20100203','20100323')insert into test1(bh,dt1,dt2) values('201','20100324','20101216') insert into test1(bh,dt1,dt2) values('201','20101217','99999999') insert into test1(bh,dt1,dt2) values('202','20100612','20100716') insert into test1(bh,dt1,dt2) values('202','20100718','99999999')insert into test1(bh,dt1,dt2) values('203','20100518','99999999')insert into test1(bh,dt1,dt2) values('204','20100412','20100510') insert into test1(bh,dt1,dt2) values('204','20100511','20100615')insert into test1(bh,dt1,dt2) values('204','20100616','20100718') insert into test1(bh,dt1,dt2) values('204','20100719','99999999')create table test2(bh varchar(10),dt1 varchar(10),dt2 varchar(10),sz int)--编号 开始日期 结束日期 数值insert into test2(bh,sz) values('201',80)insert into test2(bh,sz) values('202',20)insert into test2(bh,sz) values('204',30)*/
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
503
import
I
I
回复
0
查看
547
import
I
I
回复
0
查看
715
import
I
顶部