当前位置: 代码迷 >> Sql Server >> 请问-获取当前ID及之前2位ID对应数据(字符)的方法
  详细解决方案

请问-获取当前ID及之前2位ID对应数据(字符)的方法

热度:64   发布时间:2016-04-27 17:36:22.0
请教-获取当前ID及之前2位ID对应数据(字符)的方法
表结构    
ID         CAHAR(1)    
1             A    
2             A    
3             A    
4             B    
5             B    
6             A    
7             B    
8             B    
9             A    
10         B    
.             .    
.             .    
.             .    
从ID=3开始往下(因为只有ID> =3才能读取到三个字符),读取,把读取的值放入变量MM中。    
例如:    
ID=3     时,MM=“AAA”    
ID=4     时,MM=“AAB”    
ID=5     时,MM=“ABB”    
。    
请教思路或代码!    


------解决方案--------------------
create table ttt(id varchar(10),MM varchar(13))

insert into ttt values( '1 ', 'A ')
insert into ttt values( '2 ', 'A ')
insert into ttt values( '3 ', 'B ')
insert into ttt values( '4 ', 'A ')
insert into ttt values( '5 ', 'C ')
insert into ttt values( '6 ', 'D ')
declare @id varchar(10)
set @id= '3 '
select id,(SELECT a.MM FROM ttt a where [email protected])+(SELECT b.MM FROM ttt b where [email protected])+MM from ttt where [email protected]


  相关解决方案