一个关于sql语句delete的问题(100分)

  • 主题发起人 liuworker
  • 开始时间
L

liuworker

Unregistered / Unconfirmed
GUEST, unregistred user!
在一个表中建有如下字段:xzbh,sjbh,xjbh,yzbh,sl,rq
字段取值如下:
xzbh sjbh xjbh yzbh sl rq
"1" "1" "2" "0" 3 "2002-8-3 11:30:12"
"1" "1" "2" "1" 3 "2002-8-3 11:30:15"
"1" "1" "2" "0" 3 "2002-8-3 15:30:12"
"1" "1" "2" "1" 3 "2002-8-3 15:30:15"
现在以xzbh,sjbh,xjbh,yzbh分组,通过比较时间字段rq,将
"1" "1" "2" "0" 3 "2002-8-3 11:30:12"
"1" "1" "2" "1" 3 "2002-8-3 11:30:15"
两条记录删除,不知那位大侠可提供sql的语句,多谢
 
delete 表名 where rq='2002-8-3 11:30:12' and yzbh='0'
delete 表名 where rq='2002-8-3 11:30:15' and yzbh='1'
 
用一个临时表
create #temp { 结构相同}
insert into #temp
select * from you_table
group by ......
delete you_table
insert into you_table
select * from #temp

go

 
create procedure delcopy
as
select * into #tablename from tablenname
delete from tablename
insert into tablename select distinct xzbh,sjbh,xjbh,yzbh,sl,rq
from #tablename
 
rq不是都不一样吗?那删除还有什么难的。
Delete Talbe Where rq='''+FormatDateTime('YYYY-MM-DD HH:MM',
StrToDate(2002-8-3 11:30:12))+'''
另一条类似。
 
1、删除以xzbh,sjbh,xjbh,yzbh分组而rq最小的记录。

Delete from table1
where xzbh+sjbh+xjbh+yzbh+convert(varchar(30),rq,126) in
(select xzbh+sjbh+xjbh+yzbh+convert(varchar(30),rq,126)
from (select xzbh,sjbh,xjbh,yzbh,min(rq) as rq
from table1
group by xzbh,sjbh,xjbh,yzbh) AAA
)

2、删除所有记录,只保留以xzbh,sjbh,xjbh,yzbh分组而RQ最大的记录。
考虑重复记录不止两条的情况下只保留最新记录的情况

Delete from table1
where xzbh+sjbh+xjbh+yzbh+convert(varchar(30),rq,126) not in
(select xzbh+sjbh+xjbh+yzbh+convert(varchar(30),rq,126)
from (select xzbh,sjbh,xjbh,yzbh,max(rq) as rq
from table1
group by xzbh,sjbh,xjbh,yzbh) AAA
)

以上两句在MSSQL200下通过,其中rq为日期时间类型
 
谢谢QuickSilver的答复,可我在SYBASE数据库中还是不能调通,今天回去试试MSSQL200,
 
顶部