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(*)>1)
(二)
比方說
在A表中存在一個欄位「name」,
而且不同記錄之間的「name」值有可能會相同,
現在就是需要查詢出在該表中的各記錄之間,「name」值存在重復的項;
Select
Name,Count(*) From A Group By Name Having Count(*) > 1
如果還查性別也相同大則如下:
Select Name,sex,Count(*) From A Group By Name,sex Having
Count(*) > 1
⑵ 詳解如何刪除SQLServer表中的重復行
在這種情況下,可使用下面的方法: 1.首先,運行上面的 GROUP BY 查詢來確定有多少組重復的 PK 值及每組的重復數。 2.選擇重復的鍵值放入臨時表中。例如: SELECTcol1,col2,col3=count(*) INTOholdkey FROMt1 GROUPBYcol1,col2 HAVINGcount(*)1 3.選擇重復的行放入臨時表中,以清除進程中的重復值。例如: SELECTDISTINCTt1.* INTOholdps FROMt1,holdkey WHEREt1.col1=holdkey.col1 ANDt1.col2=holdkey.col2 SELECTcol1,col2,count(*) FROMholdps GROUPBYcol1,col2 5.從原始表中刪除重復的行。例如: DELETEt1 FROMt1,holdkey WHEREt1.col1=holdkey.col1 ANDt1.col2=holdkey.col2 6.將唯一行放回原始表中。例如:
⑶ Sql Server表裡面有2行數據完全一樣,如何刪除
Sql
Server裡面如果沒有設定主鍵而刪除重復數據很麻煩:
一:保留重復記錄中的一條記錄,其他全部刪除。
--1:建立臨時表,把不重復的數據轉存
select
distinct
*
into
#Tmp
from
表名;
--2:刪除原表數據
truncate
table
表名;
--3:將數據導回
insert
into
表名
select
*
from
#Tmp;
--4:刪除臨時表
drop
table
#Tmp;
二:刪除全部重復記錄:
delete
from
表名
where
欄位1=xxx,欄位2=xxx....(把你重復記錄的條件列在這里)
---
以上,希望對你有所幫助。
⑷ SQL查詢,如何去除重復的記錄
首先,先說明一個問題。這樣的結果出現,說明系統設計是有問題的。
其次
刪除重復數據,你要提供你是什麼資料庫。
不同資料庫會有不同的解決方案。
關鍵字Distinct 去除重復,如下列SQL,去除Test相同的記錄;
1. select distinct Test from Table
2. 如果是要刪除表中存在的重復記錄,那就邏輯處理,如下:
3. select Test from Table group by Test having count(test)>1
4. 先查詢存在重復的數據,後面根據條件刪除
還有一個更簡單的方法可以嘗試一下:
select aid, count(distinct uid) from 表名 group by aid
這是sqlserver 的寫法。
如圖一在數據表中有兩個膀胱沖洗重復的記錄。
⑸ 如何使用sql語句在sqlserver中刪除重復數據
題主可 參考下列例句:
刪除表t1欄位col1有重復的記錄
delete from t1 where exists
(select 1 from (select col1 from t1 group by col1 having count(1)>1) t where t.col1=t1.col1);
如果希望對於有重復的記錄希望保留其中一條記錄而不是全部刪除,則可以運行下列語句,前提是數據表必須含有自增id列。
delete from t1 where exists
(select 1 from (select col1,max(id) as id from t1 group by col1 having count(1)>1) t where t.col1=t1.col1 and t.id<>t1.id);
⑹ Sqlserver刪除重復記錄
delete from friend where id in (select id from
(select id,userid, row_number() over(partition by userid order by id) as rn from friend ) as a where rn > 1)
⑺ SQLserver資料庫中所有欄位全部一樣的重復數據如何刪除
找到最大的rowid即可。
Sql代碼:
alterprocgetNotDupData
as
--cleartemptable
deleteODS.dbo.Agent
deletefromstage.dbo.tmpDup
deletefromstage.dbo.tmpRowNo
deletefromstage.dbo.tmpMaxRowNo
--createptable
insertintostage.dbo.tmpDup
selectdistinctAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.dAgentPerformanceStat
'3%'orderbyAgentLogin
--addrowNo
insertintotmpRowNo
select*,ROW_NUMBER()over(orderbyAgentLogin)asrownofromtmpDup
--getmaxrowno
insertintostage.dbo.tmpMaxRowNo
selectmax(rowno)as'rowno'fromstage.dbo.(*)>1
--removemaxrowno
deletefromstage.dbo.tmpRowNowhererownoin(select*fromstage.dbo.tmpMaxRowNo)
--insertintoods
insertintoODS.dbo.AgentselectAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.tmpRowNo
⑻ mysql,sqlserver資料庫去重
b. 方法:
☆根據dname分組,查找出deptno最小的。然後再查找deptno不包含剛才查出來的。這樣就查詢出了所有的重復數據(除了deptno最小的那行)
方法2
刪除重復的行
單個欄位的如果會了,多個欄位也非常簡單。就是將group by 的欄位增加為你想要的即可。
此處只寫一個,其他方法請仿照一個欄位的寫即可。
查詢結果不含指定欄位重復
2.表需要刪除重復的記錄(重復記錄保留1條),
3.查詢重復
4.1、查找表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷
4.2、刪除表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄
4.3、查找表中多餘的重復記錄(多個欄位)
4.4、刪除表中多餘的重復記錄(多個欄位),只留有rowid最小的記錄
4.5、查找表中多餘的重復記錄(多個欄位),不包含rowid最小的記錄
4.6.消除一個欄位的左邊的第一位:
4.7.消除一個欄位的右邊的第一位:
4.8.假刪除表中多餘的重復記錄(多個欄位),不包含rowid最小的記錄
查詢重復
⑼ sqlserver利用存儲過程去除重復行的sql語句
還是先上代碼吧
,可以先看
SQL語句去掉重復記錄,獲取重復記錄
復制代碼
代碼如下:
ALTER
procere
[dbo].[PROC_ITEMMASTER_GETUNIQUE]
@PAGEINDEX
INT,@uid
int,@itemnumber
varchar(50)
AS
begin
tran
--開始事務
drop
table
[ItemMaster].[dbo].[testim]
--刪除表
--把不重復記錄轉存到testim中
select
*
into
[ItemMaster].[dbo].[testim]
from
[ItemMaster].[dbo].[dat_item_master]
where
item_uid
in(select
min(item_uid)
as
item_uid
from
[ItemMaster].[dbo].[dat_item_master]
group
by
item_number)
and
status=0
select
top
10
*
from
[ItemMaster].[dbo].[testim]
where
item_uid
not
in
(select
top
(10*(@PAGEINDEX-1))
item_uid
from
[ItemMaster].[dbo].[testim])
and
owneruid=@uid
and
item_number
like
@itemnumber+'%'
--判斷是否出錯
if
@@error<>0
begin
rollback
tran
--出錯則回滾
end
else
begin
--否則提前事務
commit
tran
end
我的數據是這樣的:因為item_uid是標識列,item_number有重復的,
我想過濾成這樣:
順帶說幾個在編程的時候遇到的小問題
1.程序
出現
Could
not
find
stored
procere
找不到這個存儲過程
因為我的程序資料庫有四個,而默認連接是A,但實際要執行B庫里的存儲過程,導致出錯,
解決辦法1:可在A裡面建個一樣的存儲過程2:在執行連接的時候,替換下資料庫就行了
2.
asp.net/C#
將存儲過程中返回的數據集,填充到dataset/datatable
復制代碼
代碼如下:
SqlConnection
conn
=
new
SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());
SqlCommand
cmd
=
new
SqlCommand("Test",conn);
cmd.CommandType
=
CommandType.StoredProcere;
cmd.Parameters.Add("@MaxId",
SqlDbType.Int).Value
=
12000;
SqlDataAdapter
sda
=
new
SqlDataAdapter(cmd);
DataTable
dt
=
new
DataTable();
sda.Fill(dt);
在這感謝
http://www.cnblogs.com/liujuncm5/archive/2009/08/31/1557569.html
3.在存儲過程裡面,寫SQL語句不能動態不加order
by
功能
比如
復制代碼
代碼如下:
--·@new_orderby
是傳入參數,不能這樣寫
select
top
(10*(2-1))
item_uid
from
testim
order
by
@new_orderby
--執行這個的時候,SQL會出現
The
SELECT
item
identified
by
the
ORDER
BY
number
1
contains
a
variable
as
part
of
the
expression
identifying
a
column
position.
Variables
are
only
allowed
when
ordering
by
an
expression
referencing
a
column
name.
不過我找到解決辦法,不過很麻煩,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9328
(第二個回答用
'
sql
'進行連接)
http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html (用case
end
也行)
4.
select
into
和
insert
into
select
兩種復制文句
(這里感謝http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html)
1.INSERT
INTO
SELECT語句
語句形式為:Insert
into
Table2(field1,field2,...)
select
value1,value2,...
from
Table1
要求目標表Table2必須存在,由於目標表Table2已經存在,所以我們除了插入源表Table1的欄位外,還可以插入常量。
2.SELECT
INTO
FROM語句
語句形式為:SELECT
vale1,
value2
into
Table2
from
Table1
要求目標表Table2不存在,因為在插入時會自動創建表Table2,並將Table1中指定欄位數據復制到Table2中。
5.順便復習下常用的SQL方法語句
復制代碼
代碼如下:
declare
@name
varchar(200)
--聲明變數
set
@name='abcd;def'
--賦值
print
'exec
len
:'+Convert(varchar(10),Len(@name))
--convert(type,value)轉換,Len(value)獲取大小
print
'exec
charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)
在value中查找find的位置
print
'not
replace:'+@name
print
'exec
replace:'+Replace(@name,';','')
--用replace替換
print
'exec
substring:'+Substring(@name,0,3)--用substring截取
print
@@RowCount
--返回上一行代碼受影響的行數
作者:chenhuzi