请你能提供如下输出 [英] Please can u provide the output as below
本文介绍了请你能提供如下输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
Create table table1(id1 int,id2 int,address1 varchar(100),address2 varchar(100))
insert into table1
values(1,3,'e','f'),(1,5,'i','j'),(1,6,'k','l')
create table table2(id1 int,id2 int,[address] varchar(100))
insert into table2(id1,id2)
values(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7)
select * from table1
select * from table2
所需的查询结果:
Desired query result:
id1 id2 address
1 1 e
1 2 e
1 3 f
1 4 i
1 5 j
1 6 l
1 7 k
如果有人可以,我会非常感激帮助我以上输出。提前致谢。
谢谢&最好的问候,
Sreeram。
I would be very thankful if any one can help me with above output. Thanks in advance.
Thanks & Best Regards,
Sreeram.
推荐答案
在创建提到的表后使用下面的sql命令
use the below sql command after creating the table mentioned
select id1,id2,case when not exists (select id2 from table1 where id2=table2.id2) then isnull((select top 1 address1 from table1 where id2>table2.id2),(select top 1 address1 from table1 where id2<table2.id2 order by id2 desc)) else (select address2 from table1 where id2=table2.id2) end as address from table2
已经过测试,请运行sql
this has been tested, please run the sql
这篇关于请你能提供如下输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文