SQL 查找重复项及批量修改数据成固定格式

时间:2021-03-06 06:08:08

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select   peopleId  from   people  group  by   peopleId  having  count(peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId  in (select   peopleId  from people  group  by   peopleId   having  count(peopleId) > 1)
and rowid not in (select min(rowid) from   people  group by peopleId  having count(peopleId )>1)

3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in   (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in   (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in   (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
6、把字段2013-5-1 批量改成2013-05-01
UPDATE table
A=convert(varchar(10),convert(datetime,A,120),120)

7、数据库里时间自动计算公式(计算工龄)
(case when dateadd(month,datediff(month,[JoinDate],getdate()),[JoinDate])>getdate() then ((((ltrim((datediff(month,[JoinDate],getdate())-(1))/(12))+'年')+ltrim((datediff(month,[JoinDate],getdate())-(1))%(12)))+'月')+ltrim(datediff(day,dateadd(month,datediff(month,[JoinDate],getdate())-(1),[JoinDate]),getdate())))+'天' else ((((ltrim(datediff(month,[JoinDate],getdate())/(12))+'年')+ltrim(datediff(month,[JoinDate],getdate())%(12)))+'月')+ltrim(datediff(day,dateadd(month,datediff(month,[JoinDate],getdate()),[JoinDate]),getdate())))+'天' end)

8、Excel表里“条件查询”-“管理规划”
=$D$4:$L$2856
=AND($L4<4, $L4<>"")