一个简单的去除重复字段的SQL查询语句.docx
- 文档编号:2168513
- 上传时间:2022-10-27
- 格式:DOCX
- 页数:10
- 大小:72.29KB
一个简单的去除重复字段的SQL查询语句.docx
《一个简单的去除重复字段的SQL查询语句.docx》由会员分享,可在线阅读,更多相关《一个简单的去除重复字段的SQL查询语句.docx(10页珍藏版)》请在冰豆网上搜索。
一个简单的去除重复字段的SQL查询语句
一个简单的去除重复字段的SQL查询语句
2009-11-1617:
12
一个简单的去除重复字段的SQL查询语句
[2008-11-0416:
01:
15byrainoxu]|分类:
我的知识库
今天公司里让.Net程序修改一个程序,需要去掉输出中的重复楼盘名称,一开始想到的是Distinct,但死路不通,只能改道,最终偶在网上找到了一个思路,修改了一下就有了。
先看所有记录(这是我在测试的数据库里做的):
OK,我们这样来消除重复项:
1.
select*fromtable1asa
wherenotexists(select1fromtable1wherelogID=a.LogIDandID>a.ID)
2.
最近做一个数据库的数据导入功能,发现联合主键约束导致不能导入,原因是源表中有重复数据,但是源表中又没有主键,很是麻烦。
经过努力终于解决了,现在就来和大家分享一下,有更好的办法的可以相互交流。
有重复数据主要有一下几种情况:
1.存在两条完全相同的纪录
这是最简单的一种情况,用关键字distinct就可以去掉
example:
selectdistinct*fromtable(表名)where(条件)
2.存在部分字段相同的纪录(有主键id即唯一键)
如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及groupby分组
example:
select*fromtablewhereidin(selectmax(id)fromtablegroupby[去除重复的字段名列表,....])
3.没有唯一键ID
这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:
example:
selectidentity(int1,1)asid,*intonewtable(临时表)fromtable
select*fromnewtablewhereidin(selectmax(id)fromnewtablegroupby[去除重复的字段名列表,....])
droptablenewtable
关于一个去除重复记录的sql语句
2009-8-2416:
33
提问者:
lichuanbao1234|悬赏分:
30|浏览次数:
1075次
我要查询一个表中content字段相同的记录的详细信息。
其中每条记录都有一个标识符state,0表示未发送,1表示已发送。
我要统计所有content相同的记录的信息,包括其中已发送(state=1)的记录。
请问大家看看我这样写有什么问题?
selectdistinctcontent,name,push_date,total,e.total_sendedfrom
tbl_jingwei_pusha,
(selectcount(*)astotal_sendedfromtbl_jingwei_pushwherestate=1andcontent=a.content)e
这样查出的其他字段都是符合要求的,唯独e.total_sended的结果出问题,它显示的是表中所有state=1的记录,请问大家我要怎么改呢?
问题补充:
这个sql语句是不对的。
表a是错误的。
请大家指点迷津,我要统计content相同并且state为1的记录数目。
谢谢各位。
我就是想去掉重复记录并统计一下,只不过如果state=1的话,我要统计一下state=1的记录数。
前提是这些记录的content是相同的。
二楼回答的不对,这和我写的是一样的,a表是不能在e表中用的。
2009-8-2416:
57
最佳答案
selectdistinctcontent,name,push_date,total,sum(casestatewhen1then1when0then0end)astotal_sendedfromtbl_jingwei_push
以上,希望对你有所帮助!
selectdistinctcontent,name,push_date,total,e.total_sendedfrom
tbl_jingwei_pusha,
(selectcount(*)astotal_sendedfromtbl_jingwei_pushwherestate=1ande.content=a.content)e
赞同
0
|评论
2009-8-2416:
54hrhero|五级
selectdistinctcontent,name,push_date,total,e.total_sendedfrom
tbl_jingwei_pusha,
(selectcount(*)astotal_sendedfromtbl_jingwei_pushwherestate=1andcontent=a.contentgroupbycontent)e
试试这样
SQLServer:
Distinct和Groupby去除重复字段记录
2010-10-1411:
31:
27| 分类:
默认分类| 标签:
|字号大中小 订阅
重复记录有两个意义,一是完全重复的记录,也即所有字段均重复的记录
二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。
1、对于第一种重复,比较容易解决,使用
selectdistinct*fromtableName
就可以得到无重复记录的结果集。
如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除
selectdistinct*into#TmpfromtableName
droptabletableName
select*intotableNamefrom#Tmp
droptable#Tmp
发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。
2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下
假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集
selectidentity(int,1,1)asautoID,*into#TmpfromtableName
selectmin(autoID)asautoIDinto#Tmp2from#TmpgroupbyName
select*from#TmpwhereautoIDin(selectautoIDfrom#tmp2)
最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)
其它的数据库可以使用序列,如:
createsequenceseq1;
selectseq1.nextvalasautoID,*into#TmpfromtableName
============
zuolo:
我根据上面实例得到所需要的语句为SELECTMAX(id)ASID,Prodou_id,FinalDyeFROManwell..tblDBDdataGROUPBYProdou_id,FinalDyeORDERBYid,之前一直想用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
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 specified 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.
DCount(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 calculation 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 ca
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一个 简单 去除 重复 字段 SQL 查询 语句