Calculated Columns in Access - ms-access

I have two columns in a table (Access DB):
Open_DATE
Closed_Date
I would like to define a third column in a table which calculates the difference in days taking the difference between the above mantioned dates.
What is the best practice to do this in Access?
Thank you!

Use a query:
Select
Open_Date,
Closed_Date,
DateDiff("d", Open_Date, Closed_Date) As Days
From
YourTableName

Using SQL:
Select [{YourTableName}]![Open_Date]-[{YourTableName}]![Closed_Date]
as {whatever you want to name the calculated field} from [{YourTableName}];

Related

MySQL update a column's values to equal the sum of another column's values based on date and another column's value

I took a screenshot of a portion of the table and added descriptors for what it is I am trying to do exactly. Any help would be appreciated.
You need SUM() aggregation with Group By on team and game_date:
SELECT
team,
game_date,
SUM(minutes) AS team_minutes_played
FROM
your_table_name
GROUP BY
team,
game_date

mysql time difference on different row

and I need to get the time difference for the day between them. Is there an easy way to do this without using TEMP TABLE?
Thanks in advance
select timediff(min(time_out), min(time_in)) as time_diff
from your_table
group by emp_id, `date`

Order By Date Difference Between Two Date Columns

I have a table that has two datetime columns (one for start time and one for end time).
I need to be able to select all entries and order them based on the time difference between these two columns (the period between the start and end time columns)
Try this::
select * from table order by TIMEDIFF(to, from)
SELECT ...
FROM ...
ORDER BY DATEDIFF(endDate, starDate);
[edit]
The above query works with dates. As pointed out by Sashi Kant, it would ignore the time part of your DATETIME
On the other hand, TIMEDIFF fails if the difference is outside of the TIME range (-838:59:59 to 838:59:59).
A complete solution could be:
SELECT ...
FROM ...
ORDER BY
DATEDIFF(endDate, starDate),
TIMEDIFF(TIME(endDate), TIME(starDate));
Probably performs terribly, though... If such precision is required, and if the difference between the two dates may be outside of the TIME range, then splitting each of your DATETIME columns into two DATE and TIME columns would certainly perform better, assuming you would apply one index on each of the four resulting columns.
SELECT something FROM table ORDER BY TIMEDIFF(end_date, start_date);
There are mysql defined functions to compute the difference between two timestamps, like TIME_TO_SEC and TIMEDIFF.
SELECT TIME_TO_SEC(TIMEDIFF(from_time, to_time)) diff from your_table order by diff;
This is correct: select * from table order by TIMEDIFF(to, from)
But if you are not selecting timediff in select statement you might have to add this line of code:
SET sql_mode = '';
select * from table order by TIMEDIFF(to, from)
to disable ONLY_FULL_GROUP_BY mode
For anyone who just needs the time distance between two timestamps, irrespective or without having to know which date is earlier and which date is later, you could use the following:
SELECT t.*,
TIMEDIFF( t.date_column, t.other_date_column ) AS distance
FROM table t
ORDER BY IF(distance<0, 0-distance, distance)
Or if you have a fixed/static target timestamp:
SELECT t.*,
TIMEDIFF( t.date_column, '2021-04-20' ) AS distance
FROM table t
ORDER BY IF(distance<0, 0-distance, distance)
The IF() function always returns a positive "distance" measurement of time between the two timestamps. This allowed me to find the nearest available record whether it was earlier or later than the target timestamp.

How to get the dates available in a mysql table

I am not very good with this, so I hope somebody can help me. I have a table filled with measurements. These measurements are taken every 30 seconds over the last 3 years, during most of the days. I would like to have a list with all the dates available in the table. So something like:
31//1/2010
1/2/2010
2/2/2010
4/2/2010
It does not matter how many measurements were taken on a certain day, as long as there is one then it should show the date. In the above example it shows that there are no measurements taken on 3/2/2010.
Is there a simple mysql select statement that can do this?
my table looks like this:
table1:
int id (auto, prim)
datetime measurementTime
double value1
double value2
SELECT DISTINCT DATE(`column_with_date_and_time`) AS `date`
FROM `table` ORDER BY `date`
Try this:
SELECT DISTINCT date_col FROM your_table
if your table stores date and time separately.
On the contrary, use this:
SELECT DISTINCT DATE(date_time_col) FROM your_table

How to tally column values of multiple rows with pure mysql?

hypothetic tables
user_id | hits
Can I get MySQL to return the total hits of a Select query? I know i could add them together with php or similar, just wondering if there is a pure MySQL way?
Total hits per user
SELECT userId, Sum(Hits)
FROM Table
GROUP by userId
OR
Total hits
SELECT Sum(Hits)
FROM Table
select sum(hits) from ...
Depending on the specific SQL query you're using you could either SUM a column or potentially use the 'WITH ROLLUP' GROUP modifier if you require a query-wide total.