The usage of Aggregate functions count, max, min, sum, avg, and NVL in oracle,

Source: Internet
Author: User

The usage of Aggregate functions count, max, min, sum, avg, and NVL in oracle,
Usage of Aggregate functions count, max, min, sum, avg, and NVL in oracle

The grouping function aggregate function performs computation on a column in a group of rows and returns a single value. The aggregate function ignores NULL values. Aggregate functions are often used together with the group by clause of SELECT statements, so they are also called grouping functions sometimes. This type of function is usually used in report statistics. The following shows the application of common Oracle Aggregate functions.

Grouping Functions Act on a group of data and return a value for a group of data.

Common grouping functions include:

Count


Used to calculate the number of valid data

Min

 

Returns the minimum value of a numeric column or computed column.

Select gi. ID,

Gi. game_instance_name,

Gi. draw_no,

Gi. draw_date,

Count (*) ticketNumber,--- Used to calculate the number of valid data

Nvl (min (tt. total_bets), 0) totalEntry,-- Used to calculate the minimum value of a given field. If it is NULL, it is replaced by 0.

Nvl (min (tt. total_amount), 0) TotalTurnover---- Used to calculate the minimum value of a given field. If it is NULL, it is replaced by 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

 

Returns the maximum value of a numeric column or computed column.

Same as the minimum value

 

Sum

 

Returns a numeric column or calculates the total number of columns.

Select gi. ID,

Gi. game_instance_name,

Gi. draw_no,

Gi. draw_date,

Count (*) ticketNumber,--- Used to calculate the number of valid data

Nvl (sum (tt. total_bets), 0) totalEntry,-- Used to calculate the sum of a given field. If it is NULL, it is replaced by 0.

Nvl (sum (tt. total_amount), 0) TotalTurnover---- Used to calculate the sum of a given field. If it is NULL, it is replaced by 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

 

Returns the average value of a numeric column or computed column.

The usage is the same as that of Max, min, and Sum.

 

NVL Function

NVL (expr1, expr2) if the first oracle parameter is null, the value of the second parameter is displayed. If the value of the first parameter is not empty, the original value of the first parameter is displayed.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.