rt
在已经排好顺序的表中,
将后面一行数据的其中一个字段,
放在该行数据前面那行的另一个字段中,
字段是字符串类型。
该表没有序列号之类的要怎样做呢??请给出详细sql语句。
在已经排好顺序的表中,
将后面一行数据的其中一个字段,
放在该行数据前面那行的另一个字段中,
字段是字符串类型。
该表没有序列号之类的要怎样做呢??请给出详细sql语句。
解决方案 »
- 查询一个表中的第二新的数据.
- sql server 2008必须为每个存储过程指定特定的用户的可执行权限吗?
- 数据同步:客户端桌面程序如何与网站应用程序数据同步
- 求大神。。。sqlserver 中的CDC可以对链接数据库进行操作么?
- 高手请进,sql存储过程笔试题目
- 如何用标准sql实现以下功能
- HELP~~SQL Server存储进程一个小问题
- 火急,不好处理的问题?---数据库恢复后,原来建的用户登陆名不存在了,所以就不能用这个用户登陆数据库,同时他所拥有的所有对象都访问
- 高手救命!!重复数据的汇总问题---急!!!!!!!!!(用别人的账号)
- oralce的实例已不会自己启动?
- 锁住表问题------------大家帮帮忙
- binary 用字符串表示
---try
select 字段=字段+(select substring(....) from TB where flag=g.flag-1) from(
select flag=(select count(1) from TB where t.字段>=字段),* from TB T) g
declare @table table([Name] varchar(1),[PartyBranch] varchar(30),[TypeNo] varchar(30))
insert @table
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部','正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员'
select identity(int,1,1) as id ,* into #temp from @tableselect name,PartyBranch + isnull((select TypeNo from #temp where id = t.id + 1),'') as PartyBranch,
TypeNo
from #temp tdrop table #temp
--结果
----------------------------
a 第一支部积极分子 正式党员
b 第一支部正式党员 积极分子
c 第一支部正式党员 正式党员
d 第一支部正式党员 正式党员
e 第一支部正式党员 正式党员
f 第二支部正式党员 正式党员
g 第二支部积极分子 正式党员
h 第二支部正式党员 积极分子
i 第二支部 正式党员
declare @tb table([id] int,[f1] varchar(3),[f2] varchar(4))
insert @tb
select 1,'aa','' union all
select 2,'bb','' union all
select 3,'bb1','' union all
select 4,'bb2','' union all
select 5,'bb3',''select a.id, a.f1, b.f1 as f2
from @tb a left join @tb b on a.id+2 = b.id--测试结果:
/*id f1 f2
----------- ---- ----
1 aa bb1
2 bb bb2
3 bb1 bb3
4 bb2 NULL
5 bb3 NULL(所影响的行数为 5 行)*/这样对么?
name PartyBranch TypeNo
a 第一支部积极分子 正式党员
b 第一支部正式党员 积极分子
c 第一支部正式党员 正式党员
d 第一支部正式党员 正式党员
e 第一支部正式党员 正式党员
f 第二支部正式党员 正式党员
g 第二支部积极分子 正式党员
h 第二支部正式党员 积极分子
i 第二支部 正式党员
首先要加一列name_end,
然后将每行数据的后一行的name字段的数据,放到前一行的name_end里面。
就是这样。
要做成如:
name PartyBranch TypeNo name_end
a 第一支部积极分子 正式党员 b
b 第一支部正式党员 积极分子 c
c 第一支部正式党员 正式党员 d
d 第一支部正式党员 正式党员 e
e 第一支部正式党员 正式党员 f
f 第二支部正式党员 正式党员 g
g 第二支部积极分子 正式党员 h
h 第二支部正式党员 积极分子 i
i 第二支部 正式党员
3楼写的什么啊,那不是建立了一个表吗。
declare @table table([Name] varchar(1),[PartyBranch] varchar(30),[TypeNo] varchar(30))
insert @table
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部','正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员'
select identity(int,1,1) as id ,* into #temp from @tableselect name,PartyBranch ,TypeNo,
name_end = (select name from #temp where id = t.id + 1)
from #temp tdrop table #temp
--结果
-------------------------
a 第一支部 正式党员 b
b 第一支部 积极分子 c
c 第一支部 正式党员 d
d 第一支部 正式党员 e
e 第一支部 正式党员 f
f 第二支部 正式党员 g
g 第二支部 正式党员 h
h 第二支部 积极分子 i
i 第二支部 正式党员 NULL
insert @table
select 'a','第一支部','正式党员' union all
select 'b','第一支部','积极分子' union all
select 'c','第一支部','正式党员' union all
select 'd','第一支部','正式党员' union all
select 'e','第一支部','正式党员' union all
select 'f','第二支部','正式党员' union all
select 'g','第二支部','正式党员' union all
select 'h','第二支部','积极分子' union all
select 'i','第二支部','正式党员';with cte as
(
select rn=ROW_NUMBER()over(order by [name]),* from @table
)
select A.Name,a.PartyBranch,a.TypeNo,isnull(b.[name],'') as name_end
from cte a left join cte b on a.rn=b.rn-1
/*
Name PartyBranch TypeNo name_end
---- ------------------------------ ------------------------------ --------
a 第一支部 正式党员 b
b 第一支部 积极分子 c
c 第一支部 正式党员 d
d 第一支部 正式党员 e
e 第一支部 正式党员 f
f 第二支部 正式党员 g
g 第二支部 正式党员 h
h 第二支部 积极分子 i
i 第二支部 正式党员 */
if OBJECT_ID('tb') is not null drop table tb
GO
create table tb([Name] varchar(1),[PartyBranch] varchar(10),[TypeNo] varchar(10),TestCol varchar(10))
GO
insert tb
select 'a','第一支部','正式党员','' union all
select 'b','第一支部','积极分子','' union all
select 'c','第一支部','正式党员','' union all
select 'd','第一支部','正式党员','' union all
select 'e','第一支部','正式党员','' union all
select 'f','第二支部','正式党员','' union all
select 'g','第二支部','正式党员','' union all
select 'h','第二支部','积极分子','' union all
select 'i','第二支部','正式党员',''GOwith cte as(select *,ROW_NUMBER()over(order by PartyBranch Desc )as Tag from tb)
select a.*,Substring(c.TypeNo,1,2)
from tb a left join cte b on a.Name=b.Name And a.PartyBranch=b.PartyBranch left join cte c on b.Tag=c.Tag-1-----------------------------------------
Name PartyBranch TypeNo TestCol
---- ----------- ---------- ---------- ----
a 第一支部 正式党员 积极
b 第一支部 积极分子 正式
c 第一支部 正式党员 正式
d 第一支部 正式党员 正式
e 第一支部 正式党员 正式
f 第二支部 正式党员 正式
g 第二支部 正式党员 积极
h 第二支部 积极分子 正式
i 第二支部 正式党员 NULL(9 行受影响)