




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一个简单的去除重复字段的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.LogID and IDa.ID)2.最近做一个数据库的数据导入功能,发现联合主键约束导致不能导入,原因是源表中有重复数据,但是源表中又没有主键,很是麻烦。经过努力终于解决了,现在就来和大家分享一下,有更好的办法的可以相互交流。有重复数据主要有一下几种情况:1.存在两条完全相同的纪录这是最简单的一种情况,用关键字distinct就可以去掉example: select distinct * from table(表名) where (条件)2.存在部分字段相同的纪录(有主键id即唯一键)如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group 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 去除重复的字段名列表,.)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_jingwei_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的记录数。前提是这些记录的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_jingwei_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 content=a.content group by content ) e试试这样SQLServer:Distinct和Group by去除重复字段记录2010-10-14 11:31:27|分类: 默认分类 |标签: |字号大中小订阅 重复记录 有两个意义,一是完全重复的记录,也即所有字段均重复的记录 二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。1、对于第一种重复,比较容易解决,使用select distinct * from tableName就可以得到无重复记录的结果集。如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除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 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: 我根据上面实例得到所需要的语句为 SELECT 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 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 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 a parameter. criteria An optional string expression used to restrict the range of data on which the DCount function is performed. For example, criteria is often equivalent to the WHERE clause in an SQL expression, without the word WHERE. If criteria is omitted, the DCount function evaluates expr against the entire domain. Any field that is included in criteria must also be a field in domain; otherwise the DCount function returns a Null. Remarks Use the DCount function to count the number of records in a domain when you dont need to know their particular values. Although the expr argument can perform a calculation on a field, the DCount function simply tallies the number of records. The value of any calculation performed by expr is unavailable. Use the DCount function in a calculated control when you need to specify criteria to restrict the range of data on which the function is performed. For example, to display the number of orders to be shipped to California, set the ControlSource property of a text box to the following expression: =DCount(OrderID, Orders, ShipRegion = CA) If you simply want to count all records in domain without specifying any restrictions, use the Count function. Tip The Count function has been optimized to speed counting of records in queries. Use the Count function in a query expression instead of the DCount function, and set optional criteria to enforce any restrictions on the results. Use the DCount function when you must count records in a domain from within a code module or macro, or in a calculated control. You can use the DCount function to count the number of records containing a particular field that isnt in the record source on which your form or report is based. For example, you could display the number of orders in the Orders table in a calculated control on a form based on the Products table. The DCount function doesnt count records that contain Null values in the field referenced by expr unless expr is the asterisk (*) wildcard character. If you use an asterisk, the DCount function calculates the total number of records, including those that contain Null fields. The following example calculates the number of records in an Orders table. intX = DCount(*, Orders) If domain is a table with a primary key, you can also count the total number of records by setting expr to the primary key field, since there will never be a Null in the primary key field. If expr identifies multiple fields, separate the field names with a concatenation operator, either an ampersand (&) or the addition operator (+). If you use an ampersand to separate the fields, the DCount function returns the number of records containing data in any of the listed fields. If you use the addition operator, the DCount function returns only the number of records containing data in all of the listed fields. The following example demonstrates the effects of each operator when used with a field that contains data in all records (ShipName) and a field that contains no data (ShipRegion). intW = DCount(ShipName, Orders) intX = DCount(ShipRegion, Orders) intY = DCount(ShipName + ShipRegion, Orders) intZ = DCount(ShipName & ShipRegion, Orders) Note The ampersand is the preferred operator for performing string concatenation. You should avoid using the addition operator for anything other than numeric addition, unless you specifically wish to propagate Nulls through an expression. Unsaved changes to records in domain arent included when you use this function. If you want the DCount function to be based on the changed values, you must first save the changes by clicking Save Record on the Records menu, moving the focus to another record, or by using the Update method. Example The following function returns the number of orders shipped to a specified country after a specified ship date. The domain is an Orders table. Public Function OrdersCount(ByVal strCountry As String, _ ByVal dteShipDate As Date) As Integer OrdersCount = DCount(ShippedDate, Orders, _ ShipCountry = & strCountry & _ AND ShippedDate # & dteShipDate & #) End Function To call the function, use the following line of code in the Immediate window: :OrdersCount UK, #1/1/96# 我现在正在DAO里面,头都大了 就用简单的吧 CS
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年硅钢行业当前发展趋势与投资机遇洞察报告
- 2025年网络借贷行业当前发展趋势与投资机遇洞察报告
- 收纳师行业知识培训课件
- 2025年义务教育科学课程标准考试测试题库及参考答案
- 2025年矿产权评估师模拟题和答案分析
- 2025版义务教育语文课程标准理论测试题及答案【附语文新课标解读】
- 2025义务教育化学课程标准考试题库及答案
- 撇捺的硬笔书法课件
- 2025年注册税务师全真模拟测试带答案
- 2025年黑龙江省大庆市中考数学试卷
- 养生茶基础知识培训课件
- 2025年暑假反电信网络诈骗试题及答案
- 2025年医学基础知识真题(附答案)
- 新学期教学工作会议上校长讲话:把功夫下在课堂里把心思放在学生上把质量落到细节中
- 几内亚湾油气项目:中国石油的机遇、风险与战略抉择
- 电工教学空气开关课件
- 废旧钢模板翻新工艺技术方案
- 5Why原因分析方法培训
- 2025至2030中国电子产品散热器行业市场现状分析及竞争格局与投资发展报告
- 2025年中国真人CS行行业市场调研分析及投资前景预测报告
- 泌尿外科快速康复外科理念
评论
0/150
提交评论