sqldistinctinto
㈠ 批量刪除資料庫表中重復數據,但是最後要留重復數據中的一條,sql語句怎麼寫(select distinct * into 新
請問,你是不是只要保留表中重復數據的其中一條就可以了?
如果是的話,可以參考一下:
select 主鍵欄位,
count(*)
from tablename
group by 主鍵欄位
having count(*)>1;
㈡ SQL裡面如何刪除重復的記錄
deletefromTABLE_namet1
wheret1.rowid>
(selectmin(rowid)fromTABLE_namet2
wheret1.name=t2.name
groupbyname
havingcount(name)>1)
還可以新建一張中間表,將distinct記錄選出來
selectdistinct*intotemptablefromTABLE_name
deletefromTABLE_name
insertintoTABLE_nameselect*fromtemptable
㈢ SQL查詢中如何剔除重復
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
(3)sqldistinctinto擴展閱讀
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)
㈣ sql distinct的用法
先mark,再想解決方案
先上代碼,針對SQLSERVER
--inserttestdata
TRUNCATEtabletable1;
INSERTINTOTable1VALUES('ls',9,'2013-08-07')
INSERTINTOTable1VALUES('zs',2,'2013-08-09')
INSERTINTOTable1VALUES('ls',7,'2013-08-08')
INSERTINTOTable1VALUES('zs',3,'2013-08-10')
INSERTINTOTable1VALUES('ls',8,'2013-08-06')
select*from(
selectROW_NUMBER()over(
PARTITIONBYNameorderby[Date]desc
)asrn,*
fromTable1
)a
wherern=1
再上結果
rn ID Name Num Date
-------------------- ----------- ---------- ----------- ----------
1 3 ls 7 2013-08-08
1 4 zs 3 2013-08-10
思路是按名稱分組,每組按日期倒序並將每個組添加行號,最後取出行號為1的記錄即可
㈤ SQL重復數據的篩選
你要看你有哪些數據段是相同的,就根據那些相同的數據段分類。
比如說,
A B C D
1 1 1 3
1 1 1 4
1 1 1 5
(前面的insert 我就不寫了)
那就是select A,B,C,MAX(D) FROM TABLE GROUP BY A,B,C
如果是
A B C D
1 1 1 2
2 1 1 3
3 1 1 4
就是說,如果你還有一個欄位是id,主鍵的話就是
select A,B,C,MAX(D) FROM TABLE GROUP BY B,C
㈥ SQL里如何刪除重復的字元串
deletefromTABLE_namet1wheret1.rowid>(selectmin(rowid)fromTABLE_namet2wheret1.name=t2.namegroupbynamehavingcount(name)>1)
--還可以新建一張中間表,將distinct記錄選出來
selectdistinct*intotemptablefromTABLE_name
deletefromTABLE_name
insertintoTABLE_nameselect*fromtemptable
㈦ sql語句 distinct 查詢疑問
select * from 新聞表 group by classid having id=max(id)
id是新聞表(不是分類表)的自動編號,classid是分類id,對應新聞分類表的id欄位。
㈧ SQL2000用語句怎麼刪除一列裡面相同的數據
有兩個意義上的重復記錄,一是完全重復的記錄,也即所有欄位均重復的記錄,二是部分關鍵欄位重復的記錄,比如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,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
最後一個select即得到了Name,Address不重復的結果集(但多了一個autoID欄位,實際寫時可以寫在select子句中省去此列)
㈨ SQL如何去重
1、首先創建一個臨時表,用於演示sqlserver語法中的去重關鍵字distinct的使用。本文以sqlserver資料庫為例演示,
IF OBJECT_ID('tempdb..#tmp1') IS NOT NULL DROP TABLE #tmp1;
CREATE TABLE #tmp1(
Col1 varchar(50),
Col2 int
);
㈩ 關於MS SQL中distinct
select DISTINCT ([id])
so2 ,
nox
from cems_control.dbo.soot1_sort
就是要求 id, so2 nox 三個都不重復的。
盡管看上去。好像你在 id 那裡,加了個 括弧。
沒用的。
舉個例子吧
比如有下面的數據:
id so2 nox
1 1 1
1 2 3
你想 只對 id 這個欄位 distinct。 那麼 so2 與 nox 有2條記錄, 要哪一條呢?
一種做法是 使用 Group By
SELECT
id, MAX(so2), MAX(nox)
FROM
cems_control.dbo.soot1_sort
GROUP BY
id
這樣 確保 id 是唯一的了。
缺點就是,可能會破壞了行的完整性。
比如
id so2 nox
1 數學 100
1 物理 85
Group By 之後的結果,就是
id so2 nox
1 物理 100