資料庫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