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`
Related
So I have this data set (down below) and I'm simply trying to gather all data based on records in field 1 that have a count of more than 30 (meaning a distinct brand that has 30+ record entries) that's it lol!
I've been trying a lot of different distinct, count esc type of queries but I'm falling short. Any help is appreciated :)
Data Set
By using GROUP BY and HAVING you can achieve this. To select more columns remember to add them to the GROUP BY clause as well.
SELECT Mens_Brand FROM your_table
WHERE Mens_Brand IN (SELECT Mens_Brand
FROM your_table
GROUP BY Mens_Brand
HAVING COUNT(Mens_Brand)>=30)
You can simply use a window function (requires mysql 8 or mariadb 10.2) for this:
select Mens_Brand, Mens_Price, Shoe_Condition, Currency, PK
from (
select Mens_Brand, Mens_Price, Shoe_Condition, Currency, PK, count(1) over (partition by Mens_Brand) brand_count
from your_table
) counted where brand_count >= 30
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}];
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.
I have the following problem:
In mysql I have a table which contains two date columns start_date and end_date. The date format is yyyy-mm-dd. What I am trying to do is to get all data from all the rows where a specific date, lets say '2012-03-05' mateches one of these date columns or are something in between.
How can I create a good sql-query that gets the data needed? I've checked on the between statement but I don't really know if that's the best way to go. I guess this is generally a simple task but I just can't figure a good query out.
Thanks.
SELECT * FROM table WHERE start_date <= '2012-02-29' AND end_date >= '2012-02-29';
Should do it.
This is a very common way to structure your tables with ranges of dates, especially in temporal database designs. It lets you perform range-based queries very efficiently, assuming that indexes on both columns exist. You query the data like this:
select *
from mytable t
where t.start_date <= #desired_date and t.endDate > #desired_date
#desired_date is the date for which you would like to query, e.g. '2012-03-05'.
Note the <= on one side and > on the other side, without =. This is done to ensure that the from-to ranges define non-overlapping intervals.
Not sure, try something like this:
SELECT
*
FROM
mytable
WHERE
'2012-03-05' BETWEEN start_date AND end_date
SELECT * FROM mytable
WHERE '2012-03-05' BETWEEN start_date AND end_date;
So, the query I'm trying to make looks like this right now:
SELECT `jid`, MAX(`start`) as `start` FROM `dates` WHERE `start` < NOW() GROUP BY `jid`
The table dates basically holds a DATETIME value start associated with a job.
As you probably guessed, jid is the id of a job, stored in another table.
A job can have many dates
What I'm trying to accomplish is to find out what jobs have all their start dates in the past.
My thought was to select the highest values of the start dates, grouped by jid and if they were in the past, it would imply that all other dates associated with the same job are also in the past.
This query isn't working because it's checking for start dates in the past and then selecting the highest of those. This doesn't exclude the possibility of another date for the same job, lying in the future.
I have no idea how I could proceed now. Any help or clue is appreciated.
Cheers - Tom
You have to use HAVING :
SELECT jid, MAX(start) as start
FROM dates
GROUP BY jid
HAVING MAX(start) < NOW();
HAVING acts a bit like WHERE. It filters out the rows after they were selected. Usually (and actually, I can't think of any other case), you only use HAVING with aggregate functions.
(I hope you really inserted dates in the future in your table, though!)