常用sql

1、去重

select *  from daydata
where wtid||rectime in
(select wtid||rectime from daydata group by wtid||rectime having count(wtid||rectime) > 1)
and ctid not in

(select min(ctid) from daydata group by wtid||rectime     having count(wtid||rectime)>1)

(适用于postgre,其他数据库类似,总体思路:

a、wtid||rectime是连接两个字段, 表示这俩个同时重复时算重复;ms sql 用wtid+rectime ;

b、第一个子查询:select wtid||rectime from daydata group by wtid||rectime having count(wtid||rectime) > 1

     统计重复的记录的重复字段;

c、select *  from daydata
where wtid||rectime in
(select wtid||rectime from daydata group by wtid||rectime having count(wtid||rectime) > 1)

统计重复记录;

d、select min(ctid) from daydata group by wtid||rectime     having count(wtid||rectime)>1

取重复记录中的一条记录

e、ctid是唯一字段,要么是主键要么就是每个数据库常用的行号字段: ms sql 用:ROW_NUMBER() over (order by id) oracle用rownumer()

你可能感兴趣的:(常用sql)