當前位置:首頁 » 凈水方式 » sql過濾相同的數據

sql過濾相同的數據

發布時間: 2021-01-22 02:14:29

① 用SQL語句怎麼過濾重復數據

有一半是添加表的,因為我沒有你的結果集,所以拼了個表變數做為結果集
,重點在後半部分,處理邏輯是按你的想寫的,前提是如果我沒有理解錯的話
這個方法的結果集返回的是每一年的數據,年數遞增的,行數以有多少個城市為准,不過我感覺你要這樣的結果集沒有什麼意義

declare @tab table(name nvarchar(20), both int)
declare @tabtmp table(name nvarchar(20), both int)
declare @tabname table(name nvarchar(20))
declare @name nvarchar(20)
declare @both int

insert into @tab
select N'上海',1996
union
select N'上海',1997
union
select N'北京',1996
union
select N'北京', 1997

insert into @tabname
select distinct name from @tab

select top 1 @name=name from @tab order by name asc
select @both=MIN(both) from @tab

while(@name is not null)
begin
insert into @tabtmp
select @name,@both

update @tab set name='' where name=@name
set @name=null
select top 1 @name =name from @tab where name<>'' order by name asc
select top 1 @both=both from @tab where both>@both order by both asc
end

select * from @tabtmp

② SQL 過濾如何兩個表重復數據

http://..com/question/198531340.html
去這兒看下吧。抄。襲。感覺你倆像一個人。。。,一樣方法,一條語句應該是出不來的
寫個存儲過程吧,和在這兒我寫的邏輯是一樣的,其實很簡單

但是數據完整性被破壞的數據是沒有什麼意義的~!!!!

因為你這裡面是客戶和日期是一對多的關系,所以不可能不讓他重復的
你不想讓他重復就會丟失數據,那麼你數據的完整性就破壞掉了

其實你可以在你的結果集中按城市分組,先顯示所有北京的,再顯示所有上海的,然後再顯示所有其它的

每個分組只顯示第一行的客戶城市,這樣看起來方便多了

③ 如何過濾sql表中的兩列或三列都相同的數據,顯示的是相同的數據,不相同的不顯示

可以通過group by having count(*) > 1來實現
如select col1,col2 from table1 group by col1,col2 having count(*) > 1
如果表的數據列不只是col1,col2且要顯示所有版的列則權可以
select a.* from table1 a join (
select col1,col2 from table1 group by col1,col2 having count(*) > 1 ) as b
on a.col1 = b.col1 and a.col2 = b.col2

④ sql insert into select where 復製表的對於重復的數據進行過濾

insertintodbo.Person(姓名,年齡版權)
selectdistinct姓名,年齡
fromdbo.Student
wherenotexist(select1fromPersonwhereStudent.姓名=Person.姓名andStuent.年齡=Person.年齡)

⑤ sql 查詢語句 資料庫 過濾重復記錄

使用分析函數row_number(大部分資料庫的新頒布都支持),對數據按你需要的重復欄位進行編號,然回後只答取編號值為1的記錄。
類似於:
select d.*
from (
-- 按mobile, area, address, post_code對記錄進行分組排序,並且按accept_name升序排
select row_number() over (group by mobile, area, address, post_code order by accept_name) as row_idx, s.*
from dt_orders s
) d
where d.row_idx = 1

⑥ sql 如何過濾重復記錄

問題背景

在一個多表查詢的sql中正常情況下產生的數據都是唯一的,但因為資料庫中存在錯誤(某張表中存在相同的外鍵ID)導致我這邊查詢出來的數據就會有重復的問題

下面結果集中UserID:15834存在多個

參考:

MSDN: OVER 子句 (Transact-SQL)

stackoverflow sql query distinct with Row_Number

SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT

⑦ sql 過濾掉多欄位重復的記錄

-按某一欄位分組取最大(小)值所在行的數據
--(愛新覺羅.毓華(十八年風雨,守得冰山雪蓮花開) 2007-10-23於浙江杭州)
/*
數據如下:
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,a.val
/*
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 , a.val
/*
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 行受影響)
*/

⑧ sql查詢去掉重復記錄

1、打開要去掉重復數據的資料庫,這里新建一張含有重復數據的user表做示例,回如下圖所示:

⑨ sql 如何過濾相同數據

樓主用distinct肯定達不到所需效果。
可以用group by 分組,不過因為其他欄位有重復值,內只能讓其他欄位取容一個值了

sql="select test1,max(test2) as test2,max(test3) as test3,max(test4) as test4 from table1 group by test1"

⑩ sql查詢語句過濾重復數據。

SELECT Id,SiteId,InsertTime,IP,Referrer,Url
FROM
(
SELECT ROW_NUMBER()OVER(PARTITION BY IP ORDER BY Id DESC) number,
Id,SiteId,InsertTime,IP,Referrer,Url
From YourTable
)T
where number = 1

拿走不謝

熱點內容
丁度巴拉斯情人電影推薦 發布:2024-08-19 09:13:07 瀏覽:886
類似深水的露點電影 發布:2024-08-19 09:10:12 瀏覽:80
《消失的眼角膜》2電影 發布:2024-08-19 08:34:43 瀏覽:878
私人影院什麼電影好看 發布:2024-08-19 08:33:32 瀏覽:593
干 B 發布:2024-08-19 08:30:21 瀏覽:910
夜晚看片網站 發布:2024-08-19 08:20:59 瀏覽:440
台灣男同電影《越界》 發布:2024-08-19 08:04:35 瀏覽:290
看電影選座位追女孩 發布:2024-08-19 07:54:42 瀏覽:975
日本a級愛情 發布:2024-08-19 07:30:38 瀏覽:832
生活中的瑪麗類似電影 發布:2024-08-19 07:26:46 瀏覽:239