[VIP专享]一个简单的去除重复字段的SQL查询语句

合集下载

sql查询去掉重复数据的方法

sql查询去掉重复数据的方法

sql查询去掉重复数据的方法
在SQL中,你可以使用`DISTINCT`关键字来查询并去掉重复的数据。

以下是一个简单的例子:
```sql
SELECT DISTINCT column_name
FROM table_name;
```
在这个例子中,`column_name`是你想要查询的列的名称,`table_name`是你想要查询的表的名称。

`DISTINCT`关键字将返回所有不重复的行。

如果你想要根据多个列来去除重复数据,你可以在`DISTINCT`后面列出多个列名,用逗号隔开:
```sql
SELECT DISTINCT column_name1, column_name2
FROM table_name;
```
这个查询将返回在`column_name1`和`column_name2`组合上都不重复的所有行。

请注意,使用`DISTINCT`可能会影响查询的性能,特别是在大数据集上。

如果你经常需要去除重复数据,可能需要考虑使用其他方法,如使用视图或索引,或者在插入数据时进行适当的规范化,以减少重复数据的可能性。

SQL优化技巧之DISTINCT去重

SQL优化技巧之DISTINCT去重

SQL优化技巧之DISTINCT去重DISTINCT是SQL语言中常用的关键词之一,用于去除查询结果中的重复记录。

当查询结果包含重复数据时,使用DISTINCT可以去重,返回唯一的结果。

虽然DISTINCT可以方便地去重,但在处理大数据集时,性能可能会受到影响。

在实际应用中,我们经常遇到需要使用DISTINCT去重的场景,如统计不同城市的用户数量、商品销售记录、订阅用户的订阅频道等。

下面我们将重点介绍几种优化DISTINCT查询的技巧。

1.使用索引在进行DISTINCT查询时,使用适当的索引可以提高查询性能。

创建一个包含DISTINCT字段的索引,可以加快数据库对该字段的速度,从而提高查询效率。

2.使用LIMIT如果查询结果只需要返回少量记录,可以在查询中使用LIMIT限制返回的结果数量。

这样,数据库只需要找到足够数量的唯一记录,而不需要继续和排序,提高了查询性能。

3.使用UNION在一些情况下,使用UNION操作可以代替DISTINCT查询。

UNION操作可以将多个查询结果合并,并去除重复记录。

相比于使用DISTINCT,UNION有时可以更快速地达到去重的效果。

4.使用GROUPBY在一些复杂查询中,使用GROUPBY可以达到去重的目的。

通过将查询结果按照其中一列进行分组,然后使用聚合函数进行统计,即可得到唯一的结果。

5.使用临时表在有大量重复数据的情况下,可以使用临时表进行去重。

将查询结果插入到一个临时表中,然后在临时表上进行DISTINCT操作,最后将去重后的结果返回。

6.考虑业务需求在使用DISTINCT去重前,应该仔细考虑业务需求是否真的需要去重。

有时候,重复数据是由于业务上的不同维度导致的,不一定需要去除。

在确保数据准确性的前提下,根据具体需求决定是否使用DISTINCT。

此外,还有一些SQL引擎或数据库的特定优化,例如MySQL的优化技巧。

可以通过调整数据库配置、使用合适的索引和分区表等方式,进一步提高DISTINCT查询的性能。

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

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

一个简单的去除重复字段的SQL查询语句2009-11-16 17:12一个简单的去除重复字段的SQL查询语句[2008-11-04 16:01:15 by rainoxu] | 分类:我的知识库今天公司里让.Net程序修改一个程序,需要去掉输出中的重复楼盘名称,一开始想到的是Distinct,但死路不通,只能改道,最终偶在网上找到了一个思路,修改了一下就有了。

先看所有记录(这是我在测试的数据库里做的):OK,我们这样来消除重复项:1.select * from table1 as awhere not exists(select 1 from table1 where logID=a.LogID and ID>a.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 table select * 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字段相同的记录的详细信息。

SQL查询去掉重复数据

SQL查询去掉重复数据

SQL查询去掉重复数据本⽂主要总结数据库去掉重复数据的⽅法去掉重复数据的⽅法:第⼀种:distinct根据单个字段去重,能精确去重;作⽤在多个字段时,只有当这⼏个字段的完全相同时,才能去重;关键字distinct只能放在SQL语句中的第⼀个,才会起作⽤上图举例说明:图中student_name 为 test的同学有两位,不同的是班级⾸先,单个字段 -》⽤distinct对student_name 进⾏筛选,单个字段查询的话,可以看到已经将⼀个重复的test学⽣记录去掉了应⽤在多个字段时,可以看到此时两个同名的test,都被查出来;应⽤在多个字段时,只有当多个字段重复才会去重⼀般⽤来返回不重复的记录条数,返回不重复的条数(去掉test重复的,就剩下6条)第⼆种:group by + count + min 去掉重复数据没有加group by之前,有两条班级名称⼀样的数据加上group by 后,会将重复的数据去掉了count + group +min:去掉重复数据⾸先根据查出重复的数据然后再加上id不在查询结果⾥⾯的,去掉重复数据SELECT * from tb_class where classname in (SELECT classname from tb_class GROUP BY classname HAVING COUNT(classname)>1) and id NOT in (SELECT min(id) from tb_class GROUP BY classname HAVING count(classname)>1)第三种:min、max(这种⽅法在第⼆种中已经⽤到了)参考:https:///download/liangfei207/10325028https:///firstdream/p/7985584.html (较复杂)。

SQL去重的三种方法汇总

SQL去重的三种方法汇总

SQL去重的三种⽅法汇总
SQL去重的三种⽅法汇总
这⾥的去重是指:查询的时候, 不显⽰重复,并不是删除表中的重复项
1.distinct去重
注意的点:distinct
只能⼀列去重,当distinct后跟⼤于1个参数时,他们之间的关系是&&(逻辑与)关系,只有全部条件相同才会去重
弊端:当查询的字段⽐较多时,distinct会作⽤多个字段,导致去重条件增多
select distinct UserResult from Table1
2.group by去重
去重原理:将重复的⾏进⾏分组,相同的数据只显⽰第⼀⾏
弊端:使⽤group by后,所有查询字段都需要使⽤聚合函数,⽐较繁琐
select min(UserName)UserName,min(UserSex)UserSex,min(UserSubject)UserSubject,min(UserResult)UserResult from Table1 group by UserResult
3.row_number() over (parttion by 分组列 order by 排序列)
弊端:⼩孟还不知道
去重原理:现根据重复列进⾏分组,分组后再进⾏排序,不同的组序号为1,相同的组序号为2,排除为2的就达到了去重效果select *from
(
--查询出重复⾏
select *,row_number() over (partition by UserResult order by UserResult desc)num from Table1
)A
where A.num=1
这⾥安利第三个,row_number(),稳⼀些!。

使用SQL语句去掉重复的记录【两种方法】

使用SQL语句去掉重复的记录【两种方法】

使⽤SQL语句去掉重复的记录【两种⽅法】海量数据(百万以上),其中有些全部字段都相同,有些部分字段相同,怎样⾼效去除重复?如果要删除⼿机(mobilePhone),电话(officePhone),邮件(email)同时都相同的数据,以前⼀直使⽤这条语句进⾏去重:delete from 表 where id not in(select max(id) from 表 group by mobilePhone,officePhone,email )ordelete from 表 where id not in(select min(id) from 表 group by mobilePhone,officePhone,email )delete from 表 where id not in(select max(id) from 表 group by mobilePhone,officePhone,email )ordelete from 表 where id not in(select min(id) from 表 group by mobilePhone,officePhone,email )其中下⾯这条会稍快些。

上⾯这条数据对于100万以内的数据效率还可以,重复数1/5的情况下⼏分钟到⼏⼗分钟不等,但是如果数据量达到300万以上,效率骤降,如果重复数据再多点的话,常常会⼏⼗⼩时跑不完,有时候会锁表跑⼀夜都跑不完。

⽆奈只得重新寻找新的可⾏⽅法,今天终于有所收获://查询出唯⼀数据的ID,并把他们导⼊临时表tmp中select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email//查询出去重后的数据并插⼊finally表中insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)//查询出唯⼀数据的ID,并把他们导⼊临时表tmp中select min(id) as mid into tmp from 表 group by mobilePhone,officePhone,email//查询出去重后的数据并插⼊finally表中insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)效率对⽐:⽤delete⽅法对500万数据去重(1/2重复)约4⼩时。

如何在MySQL中进行数据去重和重复数据处理

如何在MySQL中进行数据去重和重复数据处理

如何在MySQL中进行数据去重和重复数据处理在MySQL中进行数据去重和重复数据处理概述:在处理大量数据的过程中,经常会遇到数据重复的情况,这不仅会浪费存储空间,还会对数据分析和处理带来困难。

因此,在MySQL数据库中进行数据去重和重复数据处理是一项重要的工作。

本文将介绍一些常用的方法和技巧来帮助你在MySQL中有效地进行数据去重和重复数据处理。

第一部分:数据去重数据去重是指从一个数据集合中删除重复的数据,以保留唯一的数据记录。

方法一:使用DISTINCT关键字在SQL查询中,可以使用DISTINCT关键字来去除重复的数据。

例如,以下查询将返回去除了重复数据的结果集:SELECT DISTINCT column1, column2, ...FROM table_name;请注意,DISTINCT关键字将根据指定的列来判断数据是否重复,因此需要根据实际的需求来选择相应的列。

方法二:使用GROUP BY子句GROUP BY子句用于根据指定的列对数据进行分组,并可用于数据去重。

例如,以下查询将返回根据column1列分组后的结果集,从而去除了重复数据:SELECT column1, column2, ...FROM table_nameGROUP BY column1;需要注意的是,使用GROUP BY子句进行数据去重时,只能选择需要去重的列,而其他列的取值可能是不确定的。

因此,在实际应用中需要注意这一点。

第二部分:重复数据处理重复数据处理是指对数据进行进一步的操作,以处理重复的数据。

方法一:使用DELETE语句在MySQL中,可以使用DELETE语句来删除重复的数据。

例如,以下语句将删除table_name表中column1、column2列有重复值的数据:DELETE FROM table_nameWHERE (column1, column2) IN (SELECT column1, column2FROM table_nameGROUP BY column1, column2HAVING COUNT(*) > 1);需要注意的是,使用DELETE语句会直接删除数据,因此在执行前一定要确保操作正确。

sql语句去除重复记录(多表连接的查询)

sql语句去除重复记录(多表连接的查询)

sql语句去除重复记录(多表连接的查询)--处理表重复记录(查询和删除)/******************************************************************************************************************************************************1、Num、Name相同的重复值记录,没有⼤⼩关系只保留⼀条2、Name相同,ID有⼤⼩关系时,保留⼤或⼩其中⼀个记录******************************************************************************************************************************************************/--1、⽤于查询重复处理记录(如果列没有⼤⼩关系时2000⽤⽣成⾃增列和临时表处理,SQL2005⽤row_number函数处理)--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert#Tselect1,N'A',N'A1'union allselect2,N'A',N'A2'union allselect3,N'A',N'A3'union allselect4,N'B',N'B1'union allselect5,N'B',N'B2'Go--I、Name相同ID最⼩的记录(推荐⽤1,2,3),⽅法3在SQl05时,效率⾼于1、2⽅法1:Select* from#T a where not exists(select1 from#T where Name= and ID<a.ID)⽅法2:select a.* from#T a join(select min(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID⽅法3:select* from#T a where ID=(select min(ID) from#T where Name=)⽅法4:select a.* from#T a join#T b on = and a.ID>=b.ID group by a.ID,,a.Memo having count(1)=1⽅法5:select* from#T a group by ID,Name,Memo having ID=(select min(ID)from#T where Name=)⽅法6:select* from#T a where(select count(1) from#T where Name= and ID<a.ID)=0⽅法7:select* from#T a where ID=(select top1 ID from#T where Name= order by ID)⽅法8:select* from#T a where ID!>all(select ID from#T where Name=)⽅法9(注:ID为唯⼀时可⽤):select* from#T a where ID in(select min(ID) from#T group by Name)--SQL2005:⽅法10:select ID,Name,Memo from(select*,min(ID)over(partition by Name) as MinID from#T a)T where ID=MinID⽅法11:select ID,Name,Memo from(select*,row_number()over(partition by Name order by ID) as MinID from#T a)T where MinID=1⽣成结果:/*ID Name Memo----------- ---- ----1 A A14 B B1(2 ⾏受影响)*/--II、Name相同ID最⼤的记录,与min相反:⽅法1:Select* from#T a where not exists(select1 from#T where Name= and ID>a.ID)⽅法2:select a.* from#T a join(select max(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID order by ID⽅法3:select* from#T a where ID=(select max(ID) from#T where Name=) order by ID⽅法4:select a.* from#T a join#T b on = and a.ID<=b.ID group by a.ID,,a.Memo having count(1)=1⽅法5:select* from#T a group by ID,Name,Memo having ID=(select max(ID)from#T where Name=)⽅法6:select* from#T a where(select count(1) from#T where Name= and ID>a.ID)=0⽅法7:select* from#T a where ID=(select top1 ID from#T where Name= order by ID desc)⽅法8:select* from#T a where ID!<all(select ID from#T where Name=)⽅法9(注:ID为唯⼀时可⽤):select* from#T a where ID in(select max(ID) from#T group by Name)--SQL2005:⽅法10:select ID,Name,Memo from(select*,max(ID)over(partition by Name) as MinID from#T a)T where ID=MinID⽅法11:select ID,Name,Memo from(select*,row_number()over(partition by Name order by ID desc) as MinID from#T a)T where MinID=1⽣成结果2:/*ID Name Memo----------- ---- ----3 A A35 B B2(2 ⾏受影响)*/--2、删除重复记录有⼤⼩关系时,保留⼤或⼩其中⼀个记录--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))Insert#Tselect1,N'A',N'A1'union allselect2,N'A',N'A2'union allselect3,N'A',N'A3'union allselect4,N'B',N'B1'union allselect5,N'B',N'B2'Go--I、Name相同ID最⼩的记录(推荐⽤1,2,3),保留最⼩⼀条⽅法1:delete a from#T a where exists(select1 from#T where Name= and ID<a.ID)⽅法2:delete a from#T a left join(select min(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID where b.Id is null⽅法3:delete a from#T a where ID not in(select min(ID) from#T where Name=)⽅法4(注:ID为唯⼀时可⽤):delete a from#T a where ID not in(select min(ID)from#T group by Name)⽅法5:delete a from#T a where(select count(1) from#T where Name= and ID<a.ID)>0⽅法6:delete a from#T a where ID<>(select top1 ID from#T where Name= order by ID)⽅法7:delete a from#T a where ID>any(select ID from#T where Name=)select* from#T⽣成结果:/*ID Name Memo----------- ---- ----1 A A14 B B1(2 ⾏受影响)*/--II、Name相同ID保留最⼤的⼀条记录:⽅法1:delete a from#T a where exists(select1 from#T where Name= and ID>a.ID)⽅法2:delete a from#T a left join(select max(ID)ID,Name from#T group by Name) b on = and a.ID=b.ID where b.Id is null⽅法3:delete a from#T a where ID not in(select max(ID) from#T where Name=)⽅法4(注:ID为唯⼀时可⽤):delete a from#T a where ID not in(select max(ID)from#T group by Name)⽅法5:delete a from#T a where(select count(1) from#T where Name= and ID>a.ID)>0⽅法6:delete a from#T a where ID<>(select top1 ID from#T where Name= order by ID desc)⽅法7:delete a from#T a where ID<any(select ID from#T where Name=)select* from#T/*ID Name Memo----------- ---- ----3 A A35 B B2(2 ⾏受影响)*/--3、删除重复记录没有⼤⼩关系时,处理重复值--> --> ⽣成測試數據if not object_id('Tempdb..#T') is nulldrop table#TGoCreate table#T([Num] int,[Name] nvarchar(1))Insert#Tselect1,N'A'union allselect1,N'A'union allselect1,N'A'union allselect2,N'B'union allselect2,N'B'Go⽅法1:if object_id('Tempdb..#') is not nulldrop table#Select distinct* into# from#T--排除重复记录结果集⽣成临时表#truncate table#T--清空表insert#T select* from# --把临时表#插⼊到表#T中--查看结果select* from#T/*Num Name----------- ----1 A2 B(2 ⾏受影响)*/--重新执⾏测试数据后⽤⽅法2⽅法2:alter table#T add ID int identity--新增标识列godelete a from#T a where exists(select1 from#T where Num=a.Num and Name= and ID>a.ID)--只保留⼀条记录goalter table#T drop column ID--删除标识列--查看结果select* from#T/*Num Name----------- ----1 A2 B(2 ⾏受影响)*/--重新执⾏测试数据后⽤⽅法3⽅法3:declare Roy_Cursor cursor local forselect count(1)-1,Num,Name from#T group by Num,Name having count(1)>1declare@con int,@Num int,@Name nvarchar(1)open Roy_Cursorfetch next from Roy_Cursor into@con,@Num,@Namewhile @@Fetch_status=0beginset rowcount @con;delete#T where Num=@Num and Name=@Nameset rowcount 0;fetch next from Roy_Cursor into@con,@Num,@Nameendclose Roy_Cursordeallocate Roy_Cursor--查看结果select* from#T /*Num Name ----------- ----1 A2 B(2 ⾏受影响)*/。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一个简单的去除重复字段的SQL查询语句2009-11-16 17:12一个简单的去除重复字段的SQL查询语句[2008-11-04 16:01:15 by rainoxu] | 分类:我的知识库今天公司里让.Net程序修改一个程序,需要去掉输出中的重复楼盘名称,一开始想到的是Distinct,但死路不通,只能改道,最终偶在网上找到了一个思路,修改了一下就有了。

先看所有记录(这是我在测试的数据库里做的):OK,我们这样来消除重复项:1.select * from table1 as awhere not exists(select 1 from table1 where logID=a.LogID and ID>a.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 table select * 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 fromtbl_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 fromtbl_jingwei_push a,(select count(*) as total_sended from tbl_jingwei_push where state=1 and e.content=a.content) e|评论2009-8-24 16:54 hrhero|五级select distinct content,name,push_date,total,e.total_sended fromtbl_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 tableName drop table tableName select * into tableName from #Tmp drop table #Tmp 发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下 假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name select * 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 Tele1 Yao China 1102 Zhang Moon 1103 Wang China 1104 Wang China 1105 Yao China 110根据姓名字段检索以后,能筛选出的记录有:1,3,4,5select 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.fieldSELECT id from tb where dcount("id","tb","name='"&name&"'")>1附:DCount FunctionSee Also SpecificsYou 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 Descriptionexpr 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.RemarksUse the DCount function to count the number of records in a domain when you don't 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 isn't 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 doesn't 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 aren't 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.ExampleThe 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 IntegerOrdersCount = DCount("[ShippedDate]", "Orders", _"[ShipCountry] = '" & strCountry & _"' AND [ShippedDate] > #" & dteShipDate & "#")End FunctionTo call the function, use the following line of code in the Immediate window::OrdersCount "UK", #1/1/96#我现在正在DAO里面,头都大了就用简单的吧CString strSQL;帮我把上面那一句怎么放入CString 里面?谢谢啊,已经搞定,留给后来人吧:)strSQL.Format("SELECT * from WeakTab where dcount(%c*%c,%cWeakTab%c,%cname='%c&name&%c'%c)>1",34,34,34,34,34,34,34,34);比较麻烦的sql语句,分页的同时要去除某个字段的重复值作者: sql语句来源: sql数据库学习网时间: 2011-05-24 阅读: 次在线投稿回应楼主:或你划定只保留熬头个siteid,后面这个字段反复的记载不要。

相关文档
最新文档