数据库count去重复数据库
① SQL Server如何利用count(*)统计一张去掉重复记录的总行数
Select count(*)from [table] where id in (select distinct max(id) from [table] where gsmc like '%碳资产%' group by stockcode)
在第二条查询Max(id)的语句中增加distinct查询出的id记录都是唯一,没有重复记录。
② SQL查询,如何去除重复的记录
sql查询去除重复值语句
sql 单表/多表查询去除重复记录
单表distinct
多表group by
group by 必须放在 order by 和 limit之前,不然会报错
************************************************************************************
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有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)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * 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(*)>
③ 我要查询数据库中的数据条数,又要去掉重复的,怎么处理
使用count(distinct 列名)
select count(*) as 记录数,count(distinct callnumber) as callNumber数,count(distinct callingnumber) as callingnumber数,count(distinct callnumber+calltime ) as callNumber_time数 from 表名
④ 数据库怎么去某一字段的重复数据
--按某一字段分组取最大(小)值所在行的数据
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go
--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/
--二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/
--三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/
--四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/
--五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/
--六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
--七,如果整行数据有重复,所有的列都相同。
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 1 a1--a的第一个值
a 3 a3:a的第三个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go
select * , px = identity(int,1,1) into tmp from tb
select m.name,m.val,m.memo from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) m where px = (select min(px) from
(
select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
) n where n.name = m.name)
drop table tb,tmp
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
(2 行受影响)
*/
--在sql server 2005中可以使用row_number函数,不需要使用临时表。
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go
select m.name,m.val,m.memo from
(
select * , px = row_number() over(order by name , val) from tb
) m where px = (select min(px) from
(
select * , px = row_number() over(order by name , val) from tb
) n where n.name = m.name)
drop table tb
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
(2 行受影响)
*/
⑤ 数据库查询去除重复数据代码问题
这是我这用的一个处理方法,你看一下是否有帮助
use XLERP
--1、查询重复数据
SELECT cInvCode, cAcc_Id,count(*)FROM Attribute GROUP BY cInvCode, cAcc_Id HAVING count(*) > 1
--2、选择重复值到临时表
SELECT cInvCode, cAcc_Id,bSale, bPurchase, bSelf, bComsume, bProcing, bService, bAccessary, bInvType, bPropertyCheck, bPlanInv, bProxyForeign, bATOModel, bCheckItem, bPTOModel, bMPS, bROP, bCutMantissa, bInvModel, iInvNCost, iTopSum, iLowSum, iSafeNum, fOutExcess, cInvABC, dSDate, dEDate, cCreatePerson, cModifyPerson, dModifyDate,col3=count(*) INTO holdkey FROM Attribute GROUP BY cInvCode, cAcc_Id,bSale, bPurchase, bSelf, bComsume, bProcing, bService, bAccessary, bInvType, bPropertyCheck, bPlanInv, bProxyForeign, bATOModel, bCheckItem, bPTOModel, bMPS, bROP, bCutMantissa, bInvModel, iInvNCost, iTopSum, iLowSum, iSafeNum, fOutExcess, cInvABC, dSDate, dEDate, cCreatePerson, cModifyPerson, dModifyDate HAVING count(*) > 1
--3、选择重复的行放入临时表中,以清除进程中的重复值
SELECT DISTINCT Attribute.* INTO holdps FROM Attribute, holdkey WHERE Attribute.cInvCode = holdkey.cInvCode AND Attribute.cAcc_Id = holdkey.cAcc_Id
--4、验证 holdps 中的各个键是否唯一
SELECT cInvCode, cAcc_Id, count(*) FROM holdps GROUP BY cInvCode, cAcc_Id
--5、从原始表中删除重复的行
DELETE Attribute FROM Attribute, holdkey WHERE Attribute.cInvCode = holdkey.cInvCode AND Attribute.cAcc_Id = holdkey.cAcc_Id
--6、将唯一行放回原始表中
INSERT Attribute SELECT * FROM holdps
--7、删除临时表
drop table holdps
drop table holdkey
⑥ SQL用count统计不重复记录的方法
SELECT COUNT(DISTINCT(COL)) FROM TABLE;
⑦ sql count时,去掉重复行
你说如图 图呢???
⑧ 求教怎么统计数据库中的重复记录总数
select count(*),统计重复的记录字段1,统计重版复的记录字权段2,...........
from biao group by 统计重复的记录字段1,统计重复的记录字段2,...........
having count(*) >1
⑨ sql 去掉重复数据 和统计
drop table T_Count
create table T_Count(iId int identity(1,1), cValue varchar(30) default '')
Go
Insert into T_Count(cValue)
Select 'a,b,c'
union all select 'b,c,d,e'
union all select 'a,c,d,f'
union all select 'a,c'
Go
select sum(case when cValue like '%a%' then 1 else 0 end) iCount_A
,sum(case when cValue like '%b%' then 1 else 0 end) iCount_B
,sum(case when cValue like '%c%' then 1 else 0 end) iCount_C
,sum(case when cValue like '%d%' then 1 else 0 end) iCount_D
,sum(case when cValue like '%e%' then 1 else 0 end) iCount_E
,sum(case when cValue like '%f%' then 1 else 0 end) iCount_F
from T_Count
Go
这样取的话就不能取出在同一行记录有重复的字符。如果在同一行有重复的字符也要加进去的话就麻烦多了
⑩ 如何数据库去重复的记录SQL
先将不重复的数据插入临时表,再将原表的数据清除,将临时表内的内容插回去容
select distinct *
into #a
from table1
delete from table1
insert into table1
select * from #a