- 爱易网页
-
MSSQL教程
- 大家帮忙看看!解决思路
日期:2014-05-19 浏览次数:20400 次
大家帮忙看看!!急啊!!!
表A
F_ID F_Name F_Flag
1 aaa 0
2 bbb 0
3 ccc 0
4 ddd 0
5 eee 0
6 fff 0
我要通过SQL语句随机抽取10%的记录,把抽中的记录的F_Flag字段改为1,没抽中的记录F_Flag改为2
比如抽中了前三条记录,结果为
表A
F_ID F_Name F_Flag
1 aaa 1
2 bbb 1
3 ccc 1
4 ddd 2
5 eee 2
6 fff 2
------解决方案--------------------
Update a set a.F_Flag=(case when b.F_ID is null then 2 else 1 end)
from 表A as a Left Join (Select TOP 10 PERCENT F_ID from 表A) as b
on a.F_ID=b.F_ID
------解决方案--------------------
declare @t table(F_ID int,F_Name varchar(4),F_Flag int)
insert into @t select 1, 'aaa ',0
insert into @t select 2, 'bbb ',0
insert into @t select 3, 'ccc ',0
insert into @t select 4, 'ddd ',0
insert into @t select 5, 'eee ',0
insert into @t select 6, 'fff ',0
Update a
set
F_Flag=(case when b.F_ID is null then 2 else 1 end)