一个简单的去除重复字段的SQL查询语句

上传人:豆浆 文档编号:30781025 上传时间:2018-02-01 格式:DOC 页数:13 大小:190KB
返回 下载 相关 举报
一个简单的去除重复字段的SQL查询语句_第1页
第1页 / 共13页
一个简单的去除重复字段的SQL查询语句_第2页
第2页 / 共13页
一个简单的去除重复字段的SQL查询语句_第3页
第3页 / 共13页
一个简单的去除重复字段的SQL查询语句_第4页
第4页 / 共13页
一个简单的去除重复字段的SQL查询语句_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《一个简单的去除重复字段的SQL查询语句》由会员分享,可在线阅读,更多相关《一个简单的去除重复字段的SQL查询语句(13页珍藏版)》请在金锄头文库上搜索。

1、一个简单的去除重复字段的 SQL查询语句 2009-11-16 17:12一个简单的去除重复字段的 SQL查询语句2008-11-04 16:01:15 by rainoxu | 分类:我的知识库今天公司里让.Net 程序修改一个程序,需要去掉输出中的重复楼盘名称,一开始想到的是 Distinct,但死路不通,只能改道,最终偶在网上找到了一个思路,修改了一下就有了。先看所有记录(这是我在测试的数据库里做的):OK,我们这样来消除重复项:1. select * from table1 as a where not exists(select 1 from table1 where logID=a

2、.LogID and IDa.ID)2.最近做一个数据库的数据导入功能,发现联合主键约束导致不能导入,原因是源表中有重复数据,但是源表中又没有主键,很是麻烦。经过努力终于解决了,现在就来和大家分享一下,有更好的办法的可以相互交流。有重复数据主要有一下几种情况:1.存在两条完全相同的纪录这是最简单的一种情况,用关键字 distinct 就可以去掉example: select distinct * from table(表名) where (条件)2.存在部分字段相同的纪录(有主键 id 即唯一键)如果是这种情况的话用 distinct 是过滤不了的,这就要用到主键 id的唯一性特点及 grou

3、p by 分组example:select * from table where id in (select max(id) from table group by 去除重复的字段名列表,.)3.没有唯一键 ID这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:example:select identity(int1,1) as id,* into newtable(临时表) from tableselect * from newtable where id in (select max(id) from newtable group by 去除重复的字段名列表

4、,.)drop table newtable关于一个去除重复记录的 sql 语句 2009-8-24 16:33 提问者:lichuanbao1234 | 悬赏分:30 | 浏览次数:1075 次我要查询一个表中 content 字段相同的记录的详细信息。其中每条记录都有一个标识符 state,0 表示未发送,1 表示已发送。我要统计所有 content 相同的记录的信息,包括其中已发送(state=1 )的记录。请问大家看看我这样写有什么问题?select distinct content,name,push_date,total,e.total_sended from tbl_jingwe

5、i_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and content=a.content) e这样查出的其他字段都是符合要求的,唯独 e.total_sended 的结果出问题,它显示的是表中所有 state=1 的记录,请问大家我要怎么改呢?问题补充: 这个 sql 语句是不对的。表 a 是错误的。请大家指点迷津,我要统计 content相同并且 state 为 1 的记录数目。谢谢各位 。我就是想去掉重复记录并统计一下,只不过如果 state=1 的话,我要统计一下 state=1

6、 的记录数。前提是这些记录的 content 是相同的。二楼回答的不对,这和我写的是一样的,a 表是不能在 e 表中用的。2009-8-24 16:57 最佳答案 select distinct content,name,push_date,total,sum(case state when 1 then 1 when 0 then 0 end) as total_sended from tbl_jingwei_push 以上,希望对你有所帮助!select distinct content,name,push_date,total,e.total_sended from tbl_jingwe

7、i_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and e.content=a.content) e 0| 评论 2009-8-24 16:54 hrhero | 五级 select distinct content,name,push_date,total,e.total_sended from tbl_jingwei_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and cont

8、ent=a.content group by content ) e试试这样SQLServer:Distinct 和 Group by 去除重复字段记录 2010-10-14 11:31:27| 分类: 默认分类 | 标签: |字号大中小 订阅 重复记录 有两个意义,一是完全重复的记录,也即所有字段均重复的记录二是部分关键字段重复的记录,比如 Name 字段重复,而其他字段不一定重复或都重复可以忽略。1 、对于第一种重复,比较容易解决,使用select distinct * from tableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留 1 条) ,可以按以

9、下方法删除select distinct * into #Tmp from tableNamedrop table tableNameselect * into tableName from #Tmpdrop table #Tmp发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下假设有重复的字段为 Name,Address,要求得到这两个字段唯一的结果集select identity(int,1,1) as autoID, * into #Tmp from tableNameselect min(autoID) as

10、autoID into #Tmp2 from #Tmp group by Nameselect * from #Tmp where autoID in(select autoID from #tmp2)最后一个 select 即得到了 Name,Address 不重复的结果集(但多了一个 autoID 字段,实际写时可以写在 select 子句中省去此列)其它的数据库可以使用序列,如:create sequence seq1;select seq1.nextval as autoID, * into #Tmp from tableName=zuolo: 我根据上面实例得到所需要的语句为 SEL

11、ECT MAX(id) AS ID,Prodou_id,FinalDye FROM anwell.tblDBDdata GROUP BY Prodou_id,FinalDye ORDER BY id,之前一直想用 Distinct 来得到指定字段不重复的记录是个误区。如何写一个 SQL 语句,能检索出所有某个字段内容有重复的记录A :比如: Name Adress Tele 1 Yao China 110 2 Zhang Moon 110 3 Wang China 110 4 Wang China 110 5 Yao China 110 根据姓名字段检索以后,能筛选出的记录有:1,3,4,5

12、select b.id from table as b,(select count(field),field from table where count(field)2 group by field) as a where a.field=b.field SELECT id from tb where dcount(id,tb,name=&name&)1 附: DCount Function See Also Specifics You can use the DCount function to determine the number of records that are in a s

13、pecified set of records (a domain). Use the DCount function in Visual Basic, a macro, a query expression, or a calculated control. For example, you could use the DCount function in a module to return the number of records in an Orders table that correspond to orders placed on a particular date. DCou

14、nt(expr, domain, criteria) The DCount function has the following arguments. Argument Description expr An expression that identifies the field for which you want to count records. It can be a string expression identifying a field in a table or query, or it can be an expression that performs a calcula

15、tion on data in that field. In expr, you can include the name of a field in a table, a control on a form, a constant, or a function. If expr includes a function, it can be either built-in or user-defined, but not another domain aggregate or SQL aggregate function. domain A string expression identifying the set of records that constitutes the domain. It can be a table name or a query name for a query that does not require

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号