oracle分組統計查詢之後,獲取數量最大的記錄

2021-06-23 07:59:07 字數 1455 閱讀 1105

total_share_his表的結構

根據 cust_acco , prd_code,  prd_code_batch 三個字段分組後查詢出last_modify_date 最大的一條記錄:

select * from

(select a.*,

row_number() over (partition by a.cust_acco, a.prd_code,a.prd_code_batch order by a. last_modify_date desc) as row_num

from total_share_his a )

where row_num = 1;

備註:row_numk=2 就是第二條記錄,row_numk between 1 and 5  靈活性比較大。

因為select中欄位必須和group by中的對應或者是租函式否則報錯,所以如果不用查出total_share_his 所有字段可以用下列語句
selecta.cust_acco, a.prd_code,a.prd_code_batch ,max(
a. last_modify_date) from
total_share_his a group by
a.cust_acco,a.prd_code,a.prd_code_batch
或者
select t.* from total_share_his  t

inner join   (select tsh.cust_acco,tsh.prd_code, tsh.prd_code_batch,max(tsh.last_modify_date) max_date

from total_share_his tsh

where tsh.last_modify_date <=20140825

group by tsh.cust_acco,  tsh.prd_code, tsh.prd_code_batch ) a 

on t.cust_acco=a. cust_acco 

and   t.prd_code=a. prd_code

and   t.prd_code_batch=a. prd_code_batch

and   t.
last_modify_date=a.
max_date

LINQ分組查詢統計

var q from p in db.products group p by p.categoryid into g select new 語句描述 linq使用group by和count得到每個categoryid中產品的數量。說明 先按categoryid歸類,取出categoryid值和各個...

Oracle分組查詢

首先要明白的一點 資料重複的時候分組才有意義。分組查詢語法 select distinct 分組欄位1 別名 分組欄位2 別名 統計函式 from 表名稱 別名 表名稱 別名 where 條件 s group by 分組欄位1 分組欄位2 order by 排序字段 asc desc 排序字段 as...

oracle 分組查詢

組函式 count 個數 sum 求和 g 平均 max 最大值 min 最小值 count 會實際的統計出表中的資料量 count 字段 如果統計的字段上不包含有 null,那麼與 count 結果相同 如果統計欄位上包含有了 null,null 不參與統計 count distinct 字段 消...