求一个sql语句

心念不空过 2009-03-19 11:36:12
表中数据是这样的
sequenceNo LocationName
1 beijing
2 shanghai
3 tianjin

如何建一个view,得到如下结果?
from to
beijing shanghai
shanghai tianjin
...全文
70 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaJiaBing 2009-03-19
  • 打赏
  • 举报
回复

declare @tb table (sequenceno int,locationname nvarchar(10))
insert into @tb select 1,'beijing'
union all select 2,'shanghai'
union all select 3,'tianjin'
select a.locationname [from] ,b.locationname [to] from @tb a join @tb b
on a.sequenceno=b.sequenceno-1

from to
---------- ----------
beijing shanghai
shanghai tianjin

(2 行受影响)


ws_hgo 2009-03-19
  • 打赏
  • 举报
回复
create table tb(sequenceNo int,LocationName varchar(8))
insert tb
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'

select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t

from to
-------- --------
beijing shanghai
shanghai tianjin
tianjin NULL

(3 行受影响)
dawugui 2009-03-19
  • 打赏
  • 举报
回复
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'
go

select m.[LocationName] , n.[LocationName] from tb m , tb n where m.[sequenceNo] = n.[sequenceNo] - 1

drop table tb

/*

LocationName LocationName
------------ ------------
beijing shanghai
shanghai tianjin

(所影响的行数为 2 行)
*/
nj_1st_excellence 2009-03-19
  • 打赏
  • 举报
回复


create table tb
(
sequenceNo int,
LocationName nvarchar(50)
)


insert tb select 1,'beijing'
insert tb select 2,'shanghai'
insert tb select 3,'tianjin'

create view fromto
as
select a.LocationName as [from], b.LocationName as [to] from tb a inner join tb b on a.sequenceNo=b.sequenceNo-1
go

select * from fromto

drop view fromto
drop table tb


from to
-------------------------------------------------- --------------------------------------------------
beijing shanghai
shanghai tianjin

(2 個資料列受到影響)

百年树人 2009-03-19
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'

---定义视图---
create view View_tb
as
select *
from
(
select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t
) tt where [to] is not null

---查询---
select * from View_tb

---结果---
from to
-------- --------
beijing shanghai
shanghai tianjin

(所影响的行数为 2 行)
htl258_Tony 2009-03-19
  • 打赏
  • 举报
回复
declare @t table(sequenceNo  int,    LocationName varchar(20) )
insert @t select 1, 'beijing'
insert @t select 2, 'shanghai'
insert @t select 3, 'tianjin'

select a.LocationName [from],b.[LocationName] [to] from @t a join @t b on a.sequenceNo=b.sequenceNo-1

from to
-------------------- --------------------
beijing shanghai
shanghai tianjin

(所影响的行数为 2 行)
百年树人 2009-03-19
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'

---查询---
select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t

---结果---
from to
-------- --------
beijing shanghai
shanghai tianjin
tianjin NULL

(所影响的行数为 3 行)
心念不空过 2009-03-19
  • 打赏
  • 举报
回复
表中数据是这样的
sequenceNo LocationName
------------- -----------------------
1 beijing
2 shanghai
3 tianjin

如何建一个view,得到如下结果?
from to
------------- -----------------
beijing shanghai
shanghai tianjin
心念不空过 2009-03-19
  • 打赏
  • 举报
回复
表中数据是这样的
sequenceNo LocationName
1 beijing
2 shanghai
3 tianjin

如何建一个view,得到如下结果?
from to
beijing shanghai
shanghai tianjin
心念不空过 2009-03-19
  • 打赏
  • 举报
回复
everyone ,thanks a lot.

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧