How to Use Arithmetic Operators in Query in Google Sheets

If you want to unleash the full potential of Google Sheets, it’s crucial to master the use of arithmetic operators in the Query function. By understanding how to apply addition (+), subtraction (-), multiplication (*), and division (/) in Query, you can take your data analysis and calculations to the next level.

Four Arithmetic Operators in Query

When it comes to performing mathematical operations in Google Sheets, you can either use the arithmetic operators or equivalent Math functions. However, the Query function offers additional benefits by allowing you to apply conditions and grouping.

The operands of the arithmetic operators in Query can be the output of appropriate aggregate functions, operators, or constants, resulting in a single numerical value. Take a look at these example formulas to grasp the power of arithmetic operators in Query:

1. Multiplication in Query

Let’s say you have a dataset containing employee names, their daily wages, and the number of days they were present. To calculate the monthly payment of each employee, you can simply multiply the wage (column B) by the number of days (column C) using the Query formula:

=Query(A1:C,"Select A, B*C")

2. Subtraction in Query

In another scenario, you might need to calculate the actual present days of employees based on the total days in a calendar month (column C) and the number of absent days (column D). The following Query formula does the job:

=Query(A1:D,"Select A,B, C-D")

3. Addition in Query

Performing addition with arithmetic operators in Query is just as straightforward. Here’s an example formula that adds allowances to the gross salary:

=Query(A1:C,"Select A,B+C")

4. Division in Query

Combining the division arithmetic operator with the DateDiff scalar function, you can calculate the total number of months each employee worked based on their joining and contract end dates:

=Query(A1:C,"Select A, datediff(C,B)/30")

It’s important to note that you can use the Datedif native worksheet function to find the total months between two dates:

=ArrayFormula(datedif(B2:B5,C2:C5,"M"))

Now that you have a solid understanding of using arithmetic operators in the Query function, let’s explore some additional tips:

Multiple Arithmetic Operators in Query

To further enhance your data analysis, consider using multiple arithmetic operators in Query. For instance, you can calculate the gross wage by subtracting the total number of absent days multiplied by the daily wage from the total days in the month:

Gross Wage = (Total Days in the Month - Absent) * Daily Wages

In Query, this can be achieved with the following formula:

=Query(A1:D,"Select A, (C-D)*B")

Grouping and Mathematical Calculations in Query

In Google Sheets Query, you can even use aggregate functions as operands for the arithmetic operators. This allows you to perform grouping and mathematical calculations simultaneously. Let’s illustrate this with an example:

Consider a dataset showing the supply status of various items. You want to calculate the actual quantity supplied (subtracting the rejected truckloads) and group the data by product. This Query formula accomplishes the task:

=query(A1:D,"Select B, Sum(C)-Sum(D) where B is not null Group by B")

To take it a step further, assume one truckload of material is equal to 45 cubic meters. You can convert the number of trucks into the quantity in cubic meters with this formula:

=query(A1:D,"Select B, (Sum(C)-Sum(D))*45 where B is not null Group by B")

Keep in mind that when using multiple arithmetic operators in Query, you may encounter errors if you try to display both the number of trucks and the quantity in cubic meters. To overcome this, you can create a combination formula that correctly utilizes multiple arithmetic operators in Query:

=Query(query(A1:D,"Select B, Sum(C)-Sum(D) where B is not null Group by B"),"Select Col1,Col2, Col2*45")

By now, you should have a solid grasp of how to utilize arithmetic operators in Query within Google Sheets. If you have any doubts or questions, feel free to post them in the comments.

For more invaluable insights on Google Sheets and data analysis, visit Crawlan.com and unlock unlimited possibilities.

Related posts