I have two columns in one of my tables called TIME_OUT and TIME_IN. These are both decimal values. I basically want to sum the difference of these columns. I have the query below that outputs the difference with no issue, but I'd like to then SUM the difference:
SELECT (TIME_IN-TIME_OUT) AS DIFF FROM TABLE_timelogs WHERE YEAR(LOG_DATE) = YEAR(NOW());
+------+
| DIFF |
+------+
| 10.0 |
| 4.0 |
| 3.0 |
+------+
Is there a way to integrate SUM into the query so that the ultimate output is 17.0? Thank you in advance!
If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.
Here's the generic SQL query to two compare columns (column1, column2) in a table (table1). mysql> select * from table1 where column1 not in (select column2 from table1); In the above query, update table1, column1 and column2 as per your requirement.
Code: SELECT SUM(total_cost) FROM purchase WHERE cate_id='CA001'; Relational Algebra Expression: MySQL SUM() function retrieves the sum value of an expression which is made up of more than one columns.
The SUM() function returns the total sum of a numeric column.
Add SUM to your query so the query reads:
SELECT SUM(TIME_IN-TIME_OUT) AS DIFF
FROM TABLE_timelogs
WHERE YEAR(LOG_DATE) = YEAR(NOW());
This adds the contents of the column 'diff'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With