请教一个很少用到的单表操作SQL语句
举例如下
CREATE TABLE #AA(uid int IDENTITY, a varchar(5),b varchar(6))
INSERT INTO #AA (a,b)
select 'A158','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A159','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A168','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A178','AAAA'
CREATE TABLE #AA(uid int IDENTITY, a varchar(5),b varchar(6))
INSERT INTO #AA (a,b)
select 'A158','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A159','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A168','AAAA' UNION ALL
select '','AABBA' UNION ALL
select '','AAXA' UNION ALL
select '','AACA' UNION ALL
select 'A178','AAAA'
select * from #AA
update t1
set a=(select top 1 a from #AA t2 where t2.uid<=t1.uid and t2.a<>'' order by a desc)
from #AA t1
select * from #AA
drop table #AA
/*
1A158AAAA
2A158AABBA
3A158AAXA
4A158AACA
5A159AAAA
6A159AABBA
7A159AAXA
8A159AACA
9A168AAAA
10A168AABBA
11A168AAXA
12A168AACA
13A178AAAA
*/