求一个mssql的sql语句 - MS-SQL Server / 基础类
判断表中是否有大于10条以上的数据,
如果存在则按最好录入的时间,删除掉多余的数据。。
保持数据表中只有10条最新的数据。。
谢谢。
select ID,count(1) from TB group by ID having count(1)>10
SQL code:
;with t as
(
select rn=row_number()over(order by date desc),*
from tb
)
select * from t where rn>10
SQL code:
if (select count(1) from [Table])>10
delete from [Table] where id not in(select top 10 id from [Table] order by 时间 desc)
SQL code:
if (select count(1) from tb)>10
delete from tb where id not in (select top 10 id from tb order by dt desc)
delete a from TB a
inner join
(select * from (select ID,DT,row=row_number() over (patition by id order by dt desc) from TB)T where row>10) b
on a.id=b.id
and a.DT=b.DT
SQL code:
if exists (select count(1) from tb)>10
begin
delete from tb where date not in (select top 10 date from tb order by date desc)
end
SQL code
{{
相关问答:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jas ......
如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP
参考:http://hi.baidu.com/toiota ......
tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......
需求如下:
学院 academy(aid,aname)
班级 class(cid,cname,aid)
学生 stu(sid,sname,aid,cid)
住宿区 region(rid,rname)
宿舍楼 build(bid,rid,bnote) bnote是‘男’/‘女’
宿舍 dorm(did,rid,bid,bedn ......