SQL Server查詢語句過濾重復的數(shù)據(jù)

字號:

 
    情況一:表中存在完全重復的的數(shù)據(jù),即所有字段內(nèi)容都是相同的
    create table #
    (用戶ID int, 姓名 varchar(10), 年齡 int )
    insert into #
    select 111, '張三', 26 union all
    select 222, '李四', 25 union all
    select 333, '王五', 30 union all
    select 111, '張三', 26
    方法: select distinct * from #
    情況2:表中存在部分數(shù)據(jù)重復的字段,即 重復數(shù)據(jù)中至少有一個字段不重復create table #(用戶ID int, 姓名 varchar(10), 年齡 int, 日期 DateTime )insert into #select 111, '張三', 26 2010-02-23 union allselect 222, '李四', 25 2010-03-13 union allselect 333, '王五', 30 2011-03-25 union allselect 111, '張三', 26 2011-07-07方法:--當兩條重,取日期大的一條
    select * from t a
    where not exists (select 1 from t where a.用戶ID=用戶ID a.姓名=姓名 and 日期>a.日期)