+ -
当前位置:首页 → 问答吧 → 求教一个问题,SQL循环执行

求教一个问题,SQL循环执行

时间:2011-11-07

来源:互联网

问题:我有一个表,其中一个是日期字段,

date 字段
2011-01-01   xxx
2011-01-01 xxx
2011-01-01 
2011-01-02
2011-01-02
....
.....
2011-11-07

我现在想按日期进行删除 ,只删除一天当中记录数的固定百分比,从2011-01-01 一直执行到2011-11-07,每天的记录数是不固定的,怎么写,谢谢

在线等

作者: wrd74   发布时间: 2011-11-07

需要得到的结果是什么?

作者: fredrickhu   发布时间: 2011-11-07

你的表中,必定还有一个可以用来区分各行的列,如果有,按可以随机删除.

作者: qianjin036a   发布时间: 2011-11-07

啥意思

作者: ssp2009   发布时间: 2011-11-07

SQL code
delete from tb where id in( --id列为唯一性列
select id from( 
select id,row_number()over(partition by date order by newid())rn,(select count(*) from tb where date=a.date)ct from tb a
)t where rn*100/ct<=80  --删除80%

作者: qianjin036a   发布时间: 2011-11-07

就是随意删除每天记录数中的固定百分比

作者: wrd74   发布时间: 2011-11-07

固定百分比是哪个字段?

作者: fredrickhu   发布时间: 2011-11-07

SQL code
这方法比较好用的方法NTILE()--按百分比删除

--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([date] Datetime)
Insert #T
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-01' union all
select '2011-01-02' union all
select '2011-01-02'
Go
delete t
from 
(Select *,NTILE(10)over(partition by [date] order by [date]) as row from #T)t
where row<=8---删除80%

select * from #T

作者: roy_88   发布时间: 2011-11-07

按天数分组,删除每天80%,以上例子

作者: roy_88   发布时间: 2011-11-07

学了

作者: iymmgd   发布时间: 2011-11-07