如果有两条记录的USENAME相同,那要比较specialty1这个字段,如果是>0的就保留第一条记录,其余的都删除。
解决方案 »
- sql语句错误 求助????
- 请问:怎么查询出字段T_Num 每种值的T_DateTime时间 最新的一条记录?**************************************
- 关于数据库的创建
- 哪位哥哥帮我看一下啊,怎么样用SQL语句选最小值啊?
- 无法识别NULL?
- 这是SQL Server 的 Bug 吗,求解释呀求解释~~
- ****去掉一个最高分,去掉一个最低分,求平均分*****
- 求一个sql(100分)
- 打开SQLSERVER企业管理器,报了这个错,哪位达人知道怎么解决,急啊,在线等~~~~
- 我想遍历一个表的中所有记录,SQL语句应该如何写?
- 请教一个sql语句
- 如何在sql语句中实现递归查询?
并且有个唯一的ID字段delete L from 表 L ,表 R where L.username=R.username and L.ID > R.ID
L 是“表”的别名
delete 表 from 表 a left join(
select id=min(id) from 表 a join(
select 字段1,aa=max(
case 字段2 when 0 then 0 else 1 end
+case 字段3 when 0 then 0 else 1 end
+case 字段4 when 0 then 0 else 1 end)
from 表 group by 字段1
)b on a.字段1=b.字段1 and b.aa=(
case 字段2 when 0 then 0 else 1 end
+case 字段3 when 0 then 0 else 1 end
+case 字段4 when 0 then 0 else 1 end)
group by b.字段1
)b on a.id=b.id
where b.id is null
create table 表(id int identity(1,1),字段1 int,字段2 int,字段3 int,字段4 int)
insert 表 select 1,0,0,0
union all select 1,0,0,0
union all select 2,0,0,0
union all select 2,4,3,1
union all select 2,0,3,0
union all select 3,4,2,8
go--删除处理
delete 表 from 表 a left join(
select id=min(id) from 表 a join(
select 字段1,aa=max(
case 字段2 when 0 then 0 else 1 end
+case 字段3 when 0 then 0 else 1 end
+case 字段4 when 0 then 0 else 1 end)
from 表 group by 字段1
)b on a.字段1=b.字段1 and b.aa=(
case 字段2 when 0 then 0 else 1 end
+case 字段3 when 0 then 0 else 1 end
+case 字段4 when 0 then 0 else 1 end)
group by b.字段1
)b on a.id=b.id
where b.id is null--显示处理结果
select 字段1,字段2,字段3,字段4 from 表
go--删除测试
drop table 表/*--测试结果
字段1 字段2 字段3 字段4
----------- ----------- ----------- -----------
1 0 0 0
2 4 3 1
3 4 2 8(所影响的行数为 3 行)
--*/