create table #temp
(
fid int,
fkid int,
flag int
)
insert into #temp values (1,1,null)
insert into #temp values (2,1,null)
insert into #temp values (3,2,null)
insert into #temp values (4,2,null)
insert into #temp values (5,2,null)
insert into #temp values (6,3,null)
insert into #temp values (7,3,null)
select * from #temp
drop table #temp
我想让fkid 为1的第一条数据 flag 为 1
我想让fkid 为2的第一条数据 flag 为 1
我想让fkid 为3的第一条数据 flag 为 1其他为0 ,求一个动态写法,fkid 不是固定的 谢谢
(
fid int,
fkid int,
flag int
)
insert into #temp values (1,1,null)
insert into #temp values (2,1,null)
insert into #temp values (3,2,null)
insert into #temp values (4,2,null)
insert into #temp values (5,2,null)
insert into #temp values (6,3,null)
insert into #temp values (7,3,null)
select * from #temp
drop table #temp
我想让fkid 为1的第一条数据 flag 为 1
我想让fkid 为2的第一条数据 flag 为 1
我想让fkid 为3的第一条数据 flag 为 1其他为0 ,求一个动态写法,fkid 不是固定的 谢谢
select fid,fkid,(case when fkid between 1 and 3 then 1 else 0 end) flag
from tb
update #temp
set flag = (case when fkid between 1 and 3 then 1 else 0 end)
update t1
set t1.flag=1
from #temp t1
where not exists(select 1 from #temp t2 where t1.fkid=t2.fkid and t1.fid>t2.fid)