表中某个指标重复,去掉重复项:

select *
from #temp
where A0107
in (select A0107 from #temp 
group by A0107
having COUNT(A0107)>1
) and id not in (
   select MIN(id) from #temp
   group by A0107
having COUNT(A0107)>1
)
select * from #temp
--实现sql server 中类似于oracle中rownum的关键字,
--我想取出符合条件的第301-310条记录,又不想取出所有数据,有没有好的解决方案?
--插入数据1,2,3,4
select identity(int,1,1) as id,* into #temp from UsrA01
select * from #temp where id between 301 and 310
--1,10插入的数据1,11,21,31,跳十个
select identity(int,1,10) as id,* into #temp1 from UsrA01
select * from #temp1 where id between 301 and 310

多个字段分组

删除表中多余的重复记录(多个字段),只留有id最小的记录的有关问题

www.MyException.Cn  网友分享于:2015-08-26  浏览:32次
 
删除表中多余的重复记录(多个字段),只留有id最小的记录的问题
1、查找表中多余的重复记录(多个字段)  
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

此句运行出错,
服务器: 消息 170,级别 15,状态 1,行 2
第 2 行: ',' 附近有语法错误。

2、删除表中多余的重复记录(多个字段),只留有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)

此句运行出错,
服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: 'a' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,行 2
在关键字 'and' 附近有语法错误。

请高手帮忙修改一下错误。
我的目的是想删除表中两个字段的内容完全一样的数据,并且保留重复记录中的最小ID的数据,其余的全部删除

------解决方案--------------------
1.

select a.* from vitae a,(select peopleId,seq from vitae group by peopleId,seq  having count(*) >  1)  b
where a.peopleId = b.peopleid and a.seq = b.seq delete from vitae a
 where not exists(select peopleId,seq,min(rowid) as rowid 
from vitae 
where peopleId=a.peopleId and seq=a.seq and rowid=a.rowid
group by peopleId,seq having count(*)> 1)

在应使用条件的上下文(在 ',' 附近)中指定了非布尔类型的表达式

多条件时候不能用in 要用exists

sql问题的更多相关文章

  1. 最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目

    最近帮客户实施的基于SQL Server AlwaysOn跨机房切换项目 最近一个来自重庆的客户找到走起君,客户的业务是做移动互联网支付,是微信支付收单渠道合作伙伴,数据库里存储的是支付流水和交易流水 ...

  2. SQL Server 大数据搬迁之文件组备份还原实战

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搬迁步骤(Procedure) 搬迁脚本(SQL Codes) ...

  3. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  4. SQL Server中的高可用性(2)----文件与文件组

        在谈到SQL Server的高可用性之前,我们首先要谈一谈单实例的高可用性.在单实例的高可用性中,不可忽略的就是文件和文件组的高可用性.SQL Server允许在某些文件损坏或离线的情况下,允 ...

  5. EntityFramework Core Raw SQL

    前言 本节我们来讲讲EF Core中的原始查询,目前在项目中对于简单的查询直接通过EF就可以解决,但是涉及到多表查询时为了一步到位就采用了原始查询的方式进行.下面我们一起来看看. EntityFram ...

  6. 从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)

    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www.cnb ...

  7. 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)

    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  8. 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)

    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://w ...

  9. 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)

    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点) 第一篇http://www.cnblogs.com/lyhabc/p/4678330.html第二篇http://www ...

  10. SQL Server on Linux 理由浅析

    SQL Server on Linux 理由浅析 今天的爆炸性新闻<SQL Server on Linux>基本上在各大科技媒体上刷屏了 大家看到这个新闻都觉得非常震精,而美股,今天微软开 ...

随机推荐

  1. linux vi查找命令

    linux vi查找命令   div id="article_content" class="article_content">  使用vi编辑器编辑长文 ...

  2. ms08-067

    http://wenku.baidu.com/view/f473355077232f60ddcca119.html

  3. JDBC自动提交和批处理操作

    今天用JDBC与数据库进行交互的时候,报错如下: *************************************************************************** ...

  4. C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)

    原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...

  5. PL/SQL developer export/import (转)

    export/import图标为灰色:原因:相关应用程序没有关联菜单栏 --> Tools --> Import Tables... --> Oracle Import Export ...

  6. bzoj1076

    好像题目有点问题?且现在决定不吃的宝物以后也不能再吃好像下次还是可以吃的,就是这样然后明显是状压dp,注意这道题明显倒推更容易因为顺推很可能从一个无效状态推到有效状态 ..,..] of double ...

  7. codevs 1222 信与信封问题(二分图的完美匹配)

    1222 信与信封问题   题目描述 Description John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出.但是,第二天John的儿子Small John将这n封信都拿出了信封. ...

  8. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  9. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  10. lightoj 1291 无向图边双联通+缩点统计叶节点

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1291 #include<cstdio> #include<cstri ...