当前位置: 代码迷 >> Sql Server >> SQL 重复键的有关问题
  详细解决方案

SQL 重复键的有关问题

热度:73   发布时间:2016-04-24 09:20:31.0
SQL 重复键的问题
本帖最后由 u014090410 于 2015-01-30 14:19:35 编辑

insert into zx_hr..k07(a0188,k0700,k0701,k0702,k0705,k0703,k0704,kguid)
select a.a0188,a.rq,a.ti,a.jh,a.b,a.c,a.d,NEWID() from
(
select distinct a0188 ,cast(left(checktime,10) as datetime) rq,cast(dbo.tim(checktime) as datetime) ti,SENSORID jh,null b,'system' c,null d
from zx_hr..a01 a01,zk..CHECKINOUT ,zk..userinfo 
where a01.a0190=zk..userinfo.BADGENUMBER and zk..checkinout.userid=zk..userinfo.userid
and cast(left(checktime,10) as datetime)>=dateadd(day,-15,cast(convert(varchar(10),getdate(),126) as datetime))
and not exists(select 1 from zx_hr..k07 k07 where k07.a0188=a01.a0188 and k07.k0700=cast(left(checktime,10) as datetime )
and k07.k0701=cast(dbo.tim(checktime) as datetime) )
) a order by A0188
------解决思路----------------------
select a0188,rq,ti,COUNT(*)
 from
(
select distinct a0188 ,cast(left(checktime,10) as datetime) rq,cast(dbo.tim(checktime) as datetime) ti,SENSORID jh,null b,'system' c,null d
from zx_hr..a01 a01,zk..CHECKINOUT ,zk..userinfo 
where a01.a0190=zk..userinfo.BADGENUMBER and zk..checkinout.userid=zk..userinfo.userid
and cast(left(checktime,10) as datetime)>=dateadd(day,-15,cast(convert(varchar(10),getdate(),126) as datetime))
and not exists(select 1 from zx_hr..k07 k07 where k07.a0188=a01.a0188 and k07.k0700=cast(left(checktime,10) as datetime )
and k07.k0701=cast(dbo.tim(checktime) as datetime) )
) a
GROUP BY a0188,rq,ti
HAVING COUNT(*)>1

看哪个主键有重复,然后按这个主键去找明细。
就是主键下 SENSORID 有不同吧。
  相关解决方案