ORACLE查詢去重保留第一條排序

時間 2022-09-15 08:50:43

1樓:

用分析函式row_number() over (partition by ... order by ...)給記錄按組編號,然後只取編號值為1的記錄,應該就是你要的結果集了。

select s.*

from (

select d.a_id, d.a_date, d.

a_name, row_number() over (partition by d.a_id order by d.a_date desc) as rowidx

from your_table d

) swhere s.rowidx = 1row_number這樣的分析函式,基本上現在主流的資料庫都有支援(版本太舊的話是沒有此功能的)。

2樓:穆亞楓

select *

from (select 表.*,rank() over(partition by a_id order by a_date desc) ct

from 表)temp

order by temp.ct,temp.a_date desc是不是就可以了?試一下,沒有實際的表。

3樓:匿名使用者

我試著寫兩個,不一定可以,因為我沒有測試。不過理論上來說應該可以。

(1)利用union all不排序的特點(這樣一般來說上面的語句在前面,後面的語句在後面,不過效率應該就完蛋了)

select * from table where a_date in(select max(a_date) from table group by a_id)

union all

select * from table where a_date not in(select max(a_date) from table group by a_id)

(2)利用分組排序,然後在排序的辦法(這樣後面的順序肯定是亂的,不知道這樣行不行。)

select a_id,a_date,a_name from

(select a_id,a_date,a_name,row_number()over(partition by a_id order by a_date desc) num from table) order by num

oracle資料庫查詢去除重複的記錄,保留其中的某一條

4樓:匿名使用者

select 賬號,戶名,開戶行,是否公有,使用者id

from a

where a.id in ('000001','000002',...) -- id引數條件

and a.是否公有='私有'

union -- 與union all不同的是union 會自動去重複

select 賬號,戶名,開戶行,是否公有,使用者id

from a

where a.id in ('000001','000002',...) -- id引數條件

and a.是否公有='公有'

and a.id not in (select id from a where a.是否公有='私有')

oracle某個欄位有重複資料,如何刪除多餘資料只保留1條

5樓:匿名使用者

1、查詢表中多餘的重覆記錄,重覆記錄是根據單個字段(peopleid)來判斷。

2、刪除表中多餘的重覆記錄,重覆記錄是根據單個字段(peopleid)來判斷,只留有rowid最小的記錄。

3、查詢表中多餘的重覆記錄(多個字段)。

4、刪除表中多餘的重覆記錄(多個字段),只留有rowid最小的記錄。

5、查詢表中多餘的重覆記錄(多個字段),不包含rowid最小的記錄。就完成了。

6樓:匿名使用者

delete from z t where t.rowid not in (select min(rowid) from z t1 where t1.id=t.

id group by id)

sql根據某乙個字段重複只取第一條資料

7樓:

使用分析函式row_number() over (partiion by ... order by ...)來進行分組編號,然後取分組標號值為1的記錄即可。

目前主流的資料庫都有支援分析函式,很好用。

其中,partition by 是指定按哪些字段進行分組,這些字段值相同的記錄將在一起編號;order by則是指定在同一組中進行編號時是按照怎樣的順序。

示例(sql server 2005或以上適用):

select s.*

from (

select *, row_number() over (partition by [手機號] order by [店鋪]) as group_idx

from table_name

) swhere s.group_idx = 1

8樓:匿名使用者

用group by 最後乙個字段 用個max()

9樓:發生等將發生

如果僅僅只是查詢出來去從,那麼就用distinctselect distinct 需要去重的列明(允許多列) from table

如果是需要在表中刪除,可以這樣處理

1、建立臨時表,將重覆記錄查詢出來去重插入到臨時表2、刪除實表中的重覆記錄

3、將臨時表中的記錄插入到實表

處理完成

10樓:匿名使用者

最簡單的 select distinct (手機號)

oracle查詢去除重複

11樓:影者東昇

select distinct id, date, amount, bill from 表名

select distinct 列名稱 from 表名稱

注意事項

如果指定了 select distinct,那麼 order by 子句中的項就必須出現在選擇列表中,否則會出現錯誤。

比如sql語句:select distinct company from orders order by company asc是可以正常執行的。

但是如果sql語句是:select distinct company from orders order by num asc是不能正確執行的,在asp中會提示「order by 子句與 (num) distinct 衝突」錯誤。

sql語句修改成:select distinct company,num from orders order by num asc可以正常執行。

oracle查詢出來的資料怎麼消除重複資料?

oracle去除重複列,並且對應行中其他欄位只取乙個值。

12樓:匿名使用者

如果對「某乙個值」沒有特殊邏輯要求的話,可以用這個sql:

select distinct 主表.姓名,(select 別名.年齡 from 表名 as 別名 where 別名.姓名= 主表.姓名 and rownum=1)

from 表名 as 主表

如果要其它邏輯,在別名所在的查詢,增加條件

13樓:匿名使用者

-- 姓名name 年齡age 表名student

select name, max(age) from student group by name;

oracle查詢去除重資料

14樓:一顆星

1、distinct 關鍵字的用法:distinct 關鍵字後面的字段組合去重 distinct 必須

select distinct id from test

結果 ;根據id 去重

select distinct id,name from test

2、group by 分組去重

select id,name from test group by id,name

結果:根據id,name 組合去重

3、row_number ()over(partition by 列 order by 列 asc | desc)方法

3.1 row_number() over(order by column asc) 先對列column按照公升序,再為每條記錄返回乙個序列號

3.2 row_number() over(partition by column1 order by column2 asc) 先按照column1分組,再對分組後的資料根據column2 公升序排列

15樓:是否忘記了一切

select distinct rowid where 表名

16樓:匿名使用者

分組後,根據rowid取就可以

oracle按月分組查詢,oracle 分組查詢

with tmp as select 張一 as names,2014年1月 as dates,90 as sorcs from dual union select 張四 as names,2014年1月 as dates,66 as sorcs from dual union select 張一 ...

oracle兩張表查詢問題,oracle兩張表聯合查詢問題

select a.編號,b.時間,b.數量 from a,b where a.標號 b.編號 and a.時間 b.時間 select a.編號,sum b.數量 from a,b where a.標號 b.編號 and a.時間 b.時間 group by a.編號 select b.from a...

sql server和oracle中查詢結果返回指定行數的語句

sql server返回指定行數查詢結果 select top 10 from talbe where a 10 order by a 進行排序後,再返回指定行數,可以返回最大的行數或最小的行數。oracle返回指定行數查詢結果 select from table where a 10 and ro...

SQL如何去重,SQL查詢,如何去除重複的記錄?

1 首先建立乙個臨時表,用於演示sqlserver語法中的去重關鍵字distinct的使用。本文以sqlserver資料庫為例演示,if object id tempdb.tmp1 is not null drop table tmp1 create table tmp1 col1 varchar ...

Oracle的查詢語句怎麼寫,在oracle資料庫中查詢語句怎麼寫

1.create user username identified by password 建使用者名稱和密碼oracle oracle 2.grant connect,resource,dba to username 授權 grant connect,resource,dba,sysdba to ...