delete from usersettings where id in (
select us.id
from usersettings us
	join (
			select a.CmsUserId,a.settingtypeid,min(a.id) maxu
			from usersettings a
				join (
						select CmsUserId,settingtypeid,count(1) cc
						from usersettings with(nolock)
						group by CmsUserId,settingtypeid
						) z on a.CmsUserId=z.CmsUserId and a.SettingTypeId=z.SettingTypeId and cc>1
			group by a.CmsUserId,a.settingtypeid
		) b on us.CmsUserId=b.CmsUserId and us.SettingTypeId=b.SettingTypeId and us.id=b.maxu
)
