DSum() and DAvg() Functions MS Access
Last Updated :
11 Sep, 2024
In Microsoft Access, domain aggregate functions like DSum()
and DAvg()
provide a powerful way to calculate summaries of data across tables or queries. These functions allow users to perform calculations dynamically based on specific criteria, making them particularly useful in forms, reports, and queries. In this article, We will learn about The DSum() and DAvg() Functions of MS Access by understanding their examples.
DSum() Function
- The
DSum()
function is primarily used in Microsoft Access.
- It calculates the sum of a set of values from a specified field in a domain (which can be a table or query) based on a given criteria.
- This function is often used in Access queries, forms and reports to calculate totals dynamically.
Syntax:
DSum (expr, domain [, criteria])
Parameters:
expr
: The field or expression to sum. This could be a field name or a calculation based on fields.
domain
: The set of records (table or query) from which to retrieve the values.
criteria
(optional): A condition that limits the data to be included in the sum. This is similar to a SQL WHERE
clause.
Returns:
It returns sum of all values in a specified set of records which satisfies the criteria. If no record satisfies the criteria argument or if domain contains no records, the DSum function returns a Null.
Examples of DSum() Function in MS Access
To understand the DSum() Function in MS Access we will see the below table called Product Details as shown below:
PRODUCT_ID |
PRODUCT_PRICE |
101 |
15000 |
102 |
10000 |
103 |
11000 |
104 |
6000 |
Example 1:
Finding the sum of all product price.
Select DSum("Product_Price", "Product Details") as Total_Price;
Output:
Example 2:
Finding the sum of product price for a given condition where the product id is less than 103.
Select DSum("Product_Price", "Product Details", "Product_Id < 103") as Total_Price;
Output:
DAvg() Function
- The
DAvg()
function is a domain aggregate function in Microsoft Access used to calculate the average of a set of values in a specified field (or expression) over a domain (such as a table or query).
- It can be useful when you want to find the average value of records that match specific criteria.
Syntax:
DAvg (expr, domain [, criteria])
Parameter:
- expression: The field (or expression) for which you want the average value.
- domain: The set of records (e.g., a table or query) where the calculation will be performed.
- criteria (optional): An optional string that specifies the conditions to filter records before calculating the average.
Returns:
It returns average of all values in a specified set of records which satisfies the criteria. If no record satisfies the criteria argument DAvg function returns a Null.
Examples of DAvg() Function in MS Access
Table – Product Details :
PRODUCT_ID |
PRODUCT_PRICE |
101 |
10000 |
102 |
20000 |
103 |
30000 |
104 |
40000 |
Example 1:
Finding the average of product price.
Select DAvg("Product_Price", "Product Details") as Avg_Price;
Output:
Example 2:
Finding the average of product price for a given condition where the product id is less than 103.
Select DAvg("Product_Price", "Product Details", "Product_Id < 103") as Avg_Price;
Output:
DSum() and DAvg() Functions MS Access
Here’s a comparison between the DSum()
and DAvg()
functions in Microsoft Access in table format:
Feature |
DSum() |
DAvg() |
Purpose |
Calculates the sum of a set of values. |
Calculates the average of a set of values. |
Use Case |
Useful for finding the total of numeric fields (e.g., total sales, total product price). |
Useful for calculating the mean or average of numeric fields (e.g., average salary, average product price). |
Syntax |
DSum(expr, domain, [criteria]) |
DAvg(expr, domain, [criteria]) |
Return Value |
Returns the sum of the values that match the criteria. |
Returns the average of the values that match the criteria. |
Null Handling |
Returns Null if no records match the criteria or the domain contains no records. |
Returns Null if no records match the criteria or the domain contains no records. |
Example (No Criteria) |
DSum("Product_Price", "Product Details") returns the total price. |
DAvg("Product_Price", "Product Details") returns the average price. |
Example (With Criteria) |
DSum("Product_Price", "Product Details", "Product_ID < 103") returns the total for products with ID < 103. |
DAvg("Product_Price", "Product Details", "Product_ID < 103") returns the average for products with ID < 103. |
Common Applications |
Calculating totals in queries, forms, and reports. |
Calculating averages in queries, forms, and reports. |
Conclusion
The DSum()
and DAvg()
functions in Microsoft Access are essential tools for users who need to calculate totals and averages dynamically within a database. By applying specific criteria, you can target particular subsets of data, making your queries, forms, and reports more efficient and insightful.
Similar Reads
DMin and DMax Functions MS Access
1.DMin() Function : DMin() Function in MS Access is used to determine the minimum values in a specified set of records (a domain). The DMin functions return the minimum values that satisfy the criteria. If expression identifies numeric data, the DMin functions return numeric values. If expression id
3 min read
DVar() and DVarP() Functions MS Access
1. DVar() Function : DVar() Function in MS Access is used to estimate variance across a set of values in a specified set of records (a domain). DVar function evaluates variance across a population sample. Syntax : DVar (expr, domain, criteria) Parameters : This method accepts three-parameter as ment
3 min read
Abs() and Avg() Function in MS Access
1. Abs() Function : In MS Access, the abs() function returns a positive (absolute) number. In this function, either a positive number or a negative number is passed, and it returns the absolute of that number. It takes a number as a parameter and it returns the positive value of that number. Syntax
1 min read
Atn() and Cos() Function in MS Access
Atn() Function : In MS Access, The Atn() function returns the arctangent of a number. It takes a number as a parameter and it will return the arc tangent of that number. It will help to perform the mathematical operation of trigonometry. Syntax : Atn(number) Example1 - SELECT Atn(50) AS AtnNum; Outp
1 min read
Chr() and Asc() Function in MS Access
1. CHR() Function : The Chr() function returns the character for the given ASCII number code. The Chr function takes an integer as a parameter and returns the respective character. It works opposite to ASC() function. Syntax - Chr(ASCII number code) Parameter - Required; An integer. Return - A chara
1 min read
Day() and Hour() Function in MS Access
1. Day() Function : Day() function returns the day of the month for a given date. In this function, it will take a date as a parameter and it will return the day of that date. The returned day will between 1 to 31. Syntax : Day(date) Example-1 : SELECT Day(#02/23/2020#); Output - 23 Example-2 : SELE
1 min read
Fix() and Format() Function in MS Access
1. Fix() Function : In MS Access, the fix() function returns the integer part of a number. In this function, a number will be pass as a parameter and it will return the integer part of that number. Syntax : Fix(number) Example-1 : SELECT Fix(-75.43) AS FixNum; Output - FixNum -75 Example-2 : SELECT
2 min read
First() and Last()Function in MS Access
1. First() Function : In MS Access, the First() function is used to return a field value from the first record in the result set returned by a query. Syntax : First(expr) Parameter : expr : It represents a string expression identifying the field that contains the data we want to use or an expression
2 min read
DateDiff() and DatePart() Function in MS Access
The DateDiff() function in MS Access calculates the difference between two dates and returns the result in specified time intervals, such as years, months, or days. The DatePart() function extracts specific components of a date, like a year, month, or day, allowing for detailed analysis and manipula
4 min read
Exp() and Count() Function in MS Access
In MS Access, functions like Exp() and Count() play vital roles in data manipulation and analysis. The Exp() function is used for mathematical computations, specifically to calculate the exponential value of a number, where e (approximately 2.718) is raised to a specified power. In this article, We
3 min read