oracle中的彙總函式count、max、min、sum、avg以及NVL函數的用法,

來源:互聯網
上載者:User

oracle中的彙總函式count、max、min、sum、avg以及NVL函數的用法,
oracle中的彙總函式count、max、min、sum、avg以及NVL函數的用法

          分組函數彙總函式對一組行中的某個列執行計算執行計算並返回單一的值。彙總函式忽略空值。彙總函式經常與 SELECT 語句的 GROUP BY 子句一同使用,所以有的時候也把其稱之為分組函數。這類函數通常應用於報表統計中,以下展示Oracle常用的彙總函式的應用。

分組函數的介紹 作用於一組資料,並對一組資料返回一個值.

常見的分組函數有:

Count


用來計算有效資料的數量 

Min

 

返回一個數字列或計算資料行的最小值

select gi.ID,

       gi.game_instance_name,

       gi.draw_no,

       gi.draw_date,

       count(*) ticketNumber,---用來計算有效資料的數量

       nvl(min(tt.total_bets),0) totalEntry,--用來計算給定欄位的最小值,如果為NULL就用0代替

       nvl(min(tt.total_amount),0) TotalTurnover----用來計算給定欄位的最小值,如果為NULL就用0代替

  from te_bg_ticket tt, BG_GAME_INSTANCE gi, game g

 where tt.BG_GAME_INSTANCE_ID = gi.ID

   and gi.game_id = g.game_id

   and gi.status =1

   and tt.ticket_type =1

   and tt.is_count_in_pool =1

   and g.game_id ='4028822f483fd59401483fe62dc4000d'

   and g.game_type_id =6

 groupby gi.ID, gi.game_instance_name, gi.draw_no, gi.draw_date

havingcount(*) >0

 orderby gi.draw_no

 

 

 

Max

 

返回一個數字列或計算資料行的最大值

和最小值用法一樣

 

Sum

 

返回一個數字列或計算資料行總和

select gi.ID,

       gi.game_instance_name,

       gi.draw_no,

       gi.draw_date,

       count(*) ticketNumber,---用來計算有效資料的數量

       nvl(sum(tt.total_bets),0) totalEntry,--用來計算給定欄位的總和,如果為NULL就用0代替

       nvl(sum(tt.total_amount),0) TotalTurnover----用來計算給定欄位的總和,如果為NULL就用0代替

  from te_bg_ticket tt, BG_GAME_INSTANCE gi, game g

 where tt.BG_GAME_INSTANCE_ID = gi.ID

   and gi.game_id = g.game_id

   and gi.status =1

   and tt.ticket_type =1

   and tt.is_count_in_pool =1

   and g.game_id ='4028822f483fd59401483fe62dc4000d'

   and g.game_type_id =6

 groupby gi.ID, gi.game_instance_name, gi.draw_no, gi.draw_date

havingcount(*) >0

 orderby gi.draw_no

 

 

 

avg

 

返回一個數字列或計算資料行的平均值

用法和Max,min,Sum一樣

 

NVL函數

NVL(expr1,expr2) 如果oracle第一個參數為空白那麼顯示第二個參數的值,如果第一個參數的值不為空白,則顯示第一個參數本來的值。





相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.