create table tb1 (id int,name varchar(100)) insert into tb1 select 1,'aaa' insert into tb1 select 2,'aaa' insert into tb1 select 3,'ccc' insert into tb1 select 4,'aaa'goselect identity(int,1,1) as idenid ,* into #temp from tb1goselect id,name,(select max(id) from #temp where name = a.name and idenid <a.idenid ) as id2,name as name2 from #temp a go /*1 aaa NULL aaa 2 aaa 1 aaa 3 ccc NULL ccc 4 aaa 2 aaa*/drop table tb1,#temp
能不能只根据tb1 来写SQL 不用这么复杂啊 还要用到临时表
select identity(int,1,1) as idenid ,* into #temp from tb1 这个没明白 select id as idenid ,* into #temp from tb1 这样行吗
(id int,name varchar(100))
insert into tb1 select 1,'aaa'
insert into tb1 select 2,'aaa'
insert into tb1 select 3,'ccc'
insert into tb1 select 4,'aaa'goselect identity(int,1,1) as idenid ,* into #temp from tb1goselect id,name,(select max(id) from #temp where name = a.name and idenid <a.idenid ) as id2,name as name2 from #temp a
go
/*1 aaa NULL aaa
2 aaa 1 aaa
3 ccc NULL ccc
4 aaa 2 aaa*/drop table tb1,#temp
来写SQL 不用这么复杂啊 还要用到临时表
这个没明白
select id as idenid ,* into #temp from tb1
这样行吗